比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母。
样例
给出 A = "ABCD"
B = "ACD"
,返回 true
给出 A = "ABCD"
B = "AABC"
, 返回 false
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
思路:Hash思想。
class Solution {
public:
/**
* @param A: A string includes Upper Case letters
* @param B: A string includes Upper Case letter
* @return: if string A contains all of the characters in B return true
* else return false
*/
bool compareStrings(string A, string B) {
int bufA[26]={0};
int bufB[26]={0};
if(B=="")
return true;
for(int i=0;i<A.length();i++)
{
int tmp=A[i]-65;
bufA[tmp]++;
}
for(int i=0;i<B.length();i++)
{
int tmp=B[i]-65;
bufB[tmp]++;
}
for(int i=0;i<26;i++)
{
if(bufA[i]<bufB[i])
return false;
}
return true;
}
};