Description

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

ForA = "ABCD",B = "ACD", returntrue.

For A = "ABCD",B = "AABC", return false.

Solution
public class Solution {
    /*
     * @param A: A string
     * @param B: A string
     * @return: if string A contains all of the characters in B return true else return false
     */
    public boolean compareStrings(String A, String B) {
        // write your code here
        int[] count = new int[26];
        for (char c : A.toCharArray()) {
            count[c - 'A']++;
        }

        for (char c : B.toCharArray()) {
            count[c - 'A']--;
            if (count[c - 'A'] < 0) {
                return false;
            }
        }

        return true;
    }
}

results matching ""

    No results matching ""