-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcdOfStrings.cpp
More file actions
28 lines (28 loc) · 918 Bytes
/
gcdOfStrings.cpp
File metadata and controls
28 lines (28 loc) · 918 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
class Solution {
public:
bool check(string &s, int len) {
if (len == s.length()) return true;
bool flag = true;
for (int i = len; i < s.length() && flag; i += len) {
for (int j = 0; j < len; ++j) {
if (s[j] != s[j + i]) {
flag = false;
break;
}
}
}
return flag;
}
string gcdOfStrings(string str1, string str2) {
if (str1.length() < str2.length())
std::swap(str1, str2);
int l1 = str1.length(), l2 = str2.length(), ans = -1;
for (int len = 1; len <= l2; ++len) {
if (l2 % len != 0 || l1 % len != 0 || str1.substr(0, len) != str2.substr(0, len))
continue;
if (check(str2, len) && check(str1, len))
ans = len;
}
return ans == -1 ? "" : str2.substr(0, ans);
}
};