[Easy] Compare strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.