Compare Strings

题目:

Compare two strings A and B, determine whether A contains all of the characters in B.

Example
For A = "ABCD", B = "ACD", return true.
For A = "ABCD", B = "AABC", return false.

分析:

基本题

解法:

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) {
        // write your code here
        if (A.size() < B.size() ) {
            return false;
        }

        vector<int> hash(256, 0);
        for (int i = 0; i < A.size(); i++) {
            hash[A[i] - 'a' + 97]++;
        }
        for (int i = 0; i < B.size(); i++) {
            hash[B[i] - 'a' + 97]--;
        }
        for (int i = 0; i < hash.size(); i++) {
            if (hash[i] < 0) {
                return false;
            }
        }
        return true;
    }
};

results matching ""

    No results matching ""