-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1071.cpp
More file actions
34 lines (29 loc) · 1 KB
/
1071.cpp
File metadata and controls
34 lines (29 loc) · 1 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
class Solution {
public:
string gcdOfStrings(string str1, string str2) {
for(int l = min(str1.length(), str2.length()); l > 0; l--){
if(str1.length() % l != 0 || str2.length() % l != 0) continue;
bool works = true;
string code = str1.substr(0, l);
for(int i = 0; i < str1.length(); i += l){
for(int j = i; j < i + l; j++){
if(str1[j] != code[j-i]){
works = false;
break;
}
} if(!works) break;
}
if(!works) continue;
for(int i = 0; i < str2.length(); i += l){
for(int j = i; j < i + l; j++){
if(str2[j] != code[j-i]){
works = false;
break;
}
}
}
if(works) return code;
}
return "";
}
};