-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCompare Strings.java
More file actions
44 lines (37 loc) · 1.27 KB
/
Compare Strings.java
File metadata and controls
44 lines (37 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
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.
Example
For A = "ABCD", B = "ABC", return true.
For A = "ABCD" B = "AABC", return false.
Tags Expand
Basic Implementation String LintCode Copyright
Thinking process:
Count the number of occurance for StringA.
Count the number of occurance for StringB.
Check if all of StringB's char# <= StringA's char# at each index.
*/
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 || A.length() < B.length()) {
return false;
}
int[] countA = new int[26];
int[] countB = new int[26];
for (int i = 0; i < A.length(); i++) {
countA[A.charAt(i) - 'A']++;
}
for (int i = 0; i < B.length(); i++) {
countB[B.charAt(i) - 'A']++;
if (countB[B.charAt(i) - 'A'] > countA[B.charAt(i) - 'A']) {
return false;
}
}
return true;
}
}