-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringMatch.java
More file actions
33 lines (29 loc) · 846 Bytes
/
Copy pathstringMatch.java
File metadata and controls
33 lines (29 loc) · 846 Bytes
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
/*
Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
eg:
stringMatch("xxcaazz", "xxbaaz") → 3
stringMatch("abc", "abc") → 2
stringMatch("abc", "axc") → 0
*/
public int stringMatch(String a, String b) {
/*
int count = 0;
int length;
if(a.length() > b.length()) length = b.length();
else length = a.length();
for(int i=0; i<length-1; i++){
if(a.substring(i,i+2).equals(b.substring(i,i+2))){
count += 1;
}
}
return count;
*/
int len = Math.min(a.length(), b.length());
int count = 0;
for (int i=0; i<len-1; i++){
if(a.substring(i,i+2).equals(b.substring(i,i+2))){
count += 1;
}
}
return count;
}