Compare two strings A and B, determine whether A contains all of the characters in B.
The characters in string A and B are all Upper Case letters.
Notice
The characters of B in A are not necessary continuous or ordered.
Example
解题思路:
For A = "ABCD"
, B = "ACD"
, return true
.
For A = "ABCD"
, B = "AABC"
, return false
.
1、查了很多网上其他人博客的写法和思路,发现自己写出的代码更简洁易懂高效哈哈哈!
2、总体思路是在A中一个一个遍历寻找B中的字母,若找到则删去A中的该字符,继续寻找B的下一个字母,直至B遍历完毕说明都找到了,返回true;若没找到则直接返回false。
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
string C = A;
for(int i=0;B[i] != '\0';i++)
{
int found = C.find(B[i]);
if(found != -1)
C.erase(found,1);
else
return false;
}
return true;
}
};