-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_CommonCharacterCount
51 lines (34 loc) · 991 Bytes
/
10_CommonCharacterCount
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
44
45
46
47
48
49
50
51
int commonCharacterCount(String s1, String s2) {
int counter = 0 ;
int[] u1 = new int[123]; //a-97 z-122
for(int i = 0 ; i < s1.length() ; i++){
int index = (int)s1.charAt(i);
u1[index]++;
}
for(int i = 0 ; i < s2.length() ; i++){
int index = (int)s2.charAt(i);
if(u1[index]>=1){
counter++;
u1[index]--;
}
}
return counter;
}
/**
Given two strings, find the number of common characters between them.
Example
For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3.
Strings have 3 common characters - 2 "a"s and 1 "c".
Input/Output
[execution time limit] 3 seconds (java)
[input] string s1
A string consisting of lowercase latin letters a-z.
Guaranteed constraints:
1 ≤ s1.length ≤ 15.
[input] string s2
A string consisting of lowercase latin letters a-z.
Guaranteed constraints:
1 ≤ s2.length ≤ 15.
[output] integer
**/