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.
Notice: The characters of B in A are not necessary continuous or ordered.
Example:
For A = "ABCD", B = "ACD", return true.
For A = "ABCD", B = "AABC", return false.
This problem is similar to Two Strings Are Anagrams.
public class Solution {
/**
* @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
*/
public boolean compareStrings(String A, String B) {
if (A == null || B == null) {
return false;
}
if (A.length() < B.length()) {
return false;
}
int[] charCount = new int[256];
for (int i = 0; i < A.length(); i++) {
charCount[A.charAt(i)]++;
}
for (int i = 0; i < B.length(); i++) {
charCount[B.charAt(i)]--;
if (charCount[B.charAt(i)] < 0) {
return false;
}
}
return true;
}
}
Hope this helps,
Michael