-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1071.java
More file actions
25 lines (24 loc) · 1.05 KB
/
1071.java
File metadata and controls
25 lines (24 loc) · 1.05 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
__________________________________________________________________________________________________
class Solution {
public String gcdOfStrings(String str1, String str2) {
if (str1.equals(str2)) return str1;
StringBuilder commonStr = new StringBuilder();
int n = Math.min(str1.length(), str2.length());
for (int i=0; i<n; i++){
if (str1.charAt(i) == str2.charAt(i)){
commonStr.append(str1.charAt(i));
} else {
break;
}
}
if (commonStr.length() != n){
return "";
} else {
StringBuilder remainder = (str1.length() < str2.length()) ?
new StringBuilder(str2.substring(n)): new StringBuilder(str1.substring(n));
return gcdOfStrings(commonStr.toString(), remainder.toString());
}
}
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________