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 Caseletters.
ExampleFor A = "ABCD", B = "ABC", return true.
For A = "ABCD" B = "AABC", return false.
public boolean compareStrings(String A, String B) {
if (A == null || B == null) {
return false;
}
if (A.length() < B.length()) {
return false;
}
if (A.length() >= 0 && B.length() == 0) {
return true;
}
boolean[] visited = new boolean[A.length()];
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
int count = 0;
for (int i = 0; i < B.length(); i++) {
for (int j = 0; j < A.length(); j++) {
if (B.charAt(i) == A.charAt(j) && !visited[j]) {
count++;
if (count == B.length()) {
return true;
}
visited[j] = true;
break;
}
}
}
return false;
}
思路:
O(n^2)遍历,consumable 用visited标志。