比较两个字符串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
* @param B: A string
* @return: if string A contains all of the characters in B return true else return false
*/
bool compareStrings(string &A, string &B)
{
// write your code here
int size = A.size();
int size1 = B.size();
if(size < size1 || (size == 0 && size1 > 0))
return false;
else if(size > 0 && size1 == 0)
return true;
//cout<<"hello"<<endl;
array<int,127> myarray;
myarray.fill(0);
int i = 0;
for(i = 0; i < size; i++)
{
myarray[A[i]]++;
if(i < size1)
myarray[B[i]]--;
}
for(i = 0; i < 127; i++)
{
if(myarray[i] < 0)
break;
}
if( i < 127)
return false;
return true;
}
};