比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
样例
给出 A = "ABCD"
B = "ACD"
,返回 true
给出 A = "ABCD"
B = "AABC"
, 返回 false
public class Solution {
/*
* @param A: A string
* @param B: A string
* @return: if string A contains all of the characters in B return true else return false
*/
public boolean compareStrings(String A, String B) {
// write your code here
int[] count = new int[128];
for(int i=0;i<A.length();i++){
count[A.charAt(i)]++;
}
for(int i=0;i<B.length();i++){
if(count[B.charAt(i)]!=0){
count[B.charAt(i)]--;
}else{
return false;
}
}
return true;
}
}
使用桶排,将A字符串的每一个字符放入相应的桶中,没有一个字符在相应的字符加一。
循环B字符串,桶中有相应的字符则--,等于0则不包括,返回false;