Title Source: http://www.lintcode.com/zh-cn/problem/compare-strings/
First put the error code, the test case did not all pass. are not necessarily sequential.
1 classSolution {2 Public:3 /**4 * @param a:a string includes Upper case letters5 * @param b:a string includes Upper case letter6 * @return: If string A contains all of the characters in B return True7 * Else return false8 */9 BOOLComparestrings (stringAstringB) {Ten //Write your code here One intLena=a.size (); A intlenb=b.size (); - intI=0, j=0, temp=0; - if(lena==0&&lenb==0) the return true; - if(lena==0) - return false; - for(; i<lenb;i++) + { - if(a[j]==B[i]) + { AJ + +; attemp++; - } - Else - { - while(j<LenA) - { inJ + +; - if(a[j]==B[i]) to { +J + +; -temp++; the Break; * } $ }Panax Notoginseng } - } the if(temp==LenB) + return true; A Else the return false; + } -};
After the modification, you can accept the following program:
1 classSolution {2 Public:3 /**4 * @param a:a string includes Upper case letters5 * @param b:a string includes Upper case letter6 * @return: If string A contains all of the characters in B return True7 * Else return false8 */9 BOOLComparestrings (stringAstringB) {Ten //Write your code here One intLena=a.size (); A intlenb=b.size (); - if(lena==0&&lenb==0) - return true; the if(lena==0) - return false; - if(lenb==0) - return true; + inttemp=0; - string:: iterator P; + for(intI=0; I<b.size (); i++) A { at for(intj=0; J<a.size (); j + +) - { - if(a[j]==B[i]) - { -temp++; -P=a.begin () +J; in a.erase (p); - Break; to } + } - } the if(temp==b.size ()) * return true; $ Else Panax Notoginseng return false; - } the};
The program that can accept 2 is as follows:
1 classSolution {2 Public:3 /**4 * @param a:a string includes Upper case letters5 * @param b:a string includes Upper case letter6 * @return: If string A contains all of the characters in B return True7 * Else return false8 */9 BOOLComparestrings (stringAstringB) {Ten intcount[ -]; One for(inti =0; I < -; i++) { ACount[i] =0; - } - for(inti =0; I < a.length (); i++) { theCount[a[i]-'A'] ++; - } - for(inti =0; I < b.length (); i++) { -Count[b[i]-'A'] --; + if(Count[b[i]-'A'] <0) { - return false; + } A } at return true; - } -};
[Easy] Compare strings