lintcode -- 比较字符串
来源:互联网 发布:vc贪吃蛇c语言代码 编辑:程序博客网 时间:2024/05/21 00:15
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
注意事项
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
给出 A = "ABCD"
B = "ACD"
,返回 true
给出 A = "ABCD"
B = "AABC"
, 返回 false
//1.常规方法
/*
2.将A,B字符串映射到大小为26的a,b数组中,数组内容为所对应字母的个数。
遍历a,b数组,如果b中无元素大于a中对应元素则返回true,反之返回false。
/*
目前思路是分别扫描两个字符串得到两个map<Character,Integer> 比较字母出现次数,若A的每个字母次数都>=B , 则成立。同理 判断是否互为变位词也可以使用这个思路,把次数判断为相等即可
*/
public class Solution {
public boolean compareStrings(String A, String B) {
int[] counts = new int[26];
for (int i = 0; i < 26; i++) {
counts[i] = 0;
}
//数组内容为所对应字母的个数。
for (int i = 0; i < A.length(); i++) {
counts[A.charAt(i) - 'A'] ++;
}
for (int i = 0; i < B.length(); i++) {
counts[B.charAt(i) - 'A'] --;
if (counts[B.charAt(i) - 'A'] < 0) {
return false;
}
}
return true;
}
}
- Lintcode(2)-比较字符串
- LintCode比较字符串
- Lintcode 比较字符串
- lintcode-比较字符串-55
- LintCode 比较字符串
- Lintcode 比较字符串
- LintCode-比较字符串
- lintcode刷题--比较字符串
- LintCode 比较字符串
- Lintcode-比较字符串
- lintcode ----比较字符串
- LintCode之比较字符串
- lintCode 55:比较字符串
- LintCode : 比较字符串
- Lintcode 比较字符串
- LintCode 54 比较字符串
- LintCode-比较字符串
- lintcode -- 比较字符串
- jcorp 头像裁剪—api—个人总结(坑)
- java中的各种修饰符作用范围
- HTTP Status 500
- 设置背景时没作用
- 枚举顺序对于算法的影响
- lintcode -- 比较字符串
- jmeter 常见问题 及解决方法
- 7.31 E
- AtCoder Regular Contest 079 C D E
- jsp九大内置对象
- STM32命名规则+F1系列数据手册
- 剑指offer:数组中出现次数超过一半的数字
- Kotlin in Action Review Points
- 【并查集】hdu 1856 More is better