Compare Strings

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.

http://www.lintcode.com/en/problem/compare-strings/

Solution

//run time: O(n)
//space: O(1)
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) {
        //unordered_map<char, int> chars;
        int chars[256] = {0};
        for(auto const& c:A) {
            chars[c]++;
        }
        for(auto const& c : B) {
            chars[c]--;
            if(chars[c] < 0) {
                return false;
            }
        }
        return true;
    }
};

results matching ""

    No results matching ""