forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1071. Greatest Common Divisor of Strings.cpp
64 lines (55 loc) · 2.1 KB
/
1071. Greatest Common Divisor of Strings.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
//Runtime: 4 ms, faster than 91.98% of C++ online submissions for Greatest Common Divisor of Strings.
//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Greatest Common Divisor of Strings.
class Solution {
public:
int greatestCommonDivisor(int x, int y){
if(x < y){
int tmp = x;
x = y;
y = tmp;
}
while(y != 0){
int org_y = y;
y = x % y;
x = org_y;
}
return x;
}
string gcdOfStrings(string str1, string str2) {
int l1 = str1.size(), l2 = str2.size();
int gcd = greatestCommonDivisor(l1, l2);
string sgcd = str1.substr(0, gcd);
// cout << sgcd << endl;
// cout << "str1" << endl;
for(int i = 0; i < l1/gcd; i++){
// cout << i*gcd << endl;
if(str1.substr(i*gcd, gcd) != sgcd)
return "";
}
// cout << "str2" << endl;
for(int i = 0; i < l2/gcd; i++){
// cout << i*gcd << endl;
if(str2.substr(i*gcd, gcd) != sgcd)
return "";
}
return sgcd;
}
};
//https://leetcode.com/problems/greatest-common-divisor-of-strings/discuss/303781/JavaPython-3-3-codes-each%3A-Recursive-iterative-and-regex-w-brief-comments-and-analysis.
//Method 3: Regular Expression
//Runtime: 16 ms, faster than 14.18% of C++ online submissions for Greatest Common Divisor of Strings.
//Memory Usage: 13.8 MB, less than 100.00% of C++ online submissions for Greatest Common Divisor of Strings.
class Solution {
public:
int greastestCommonDivisor(int x, int y){
return y == 0 ? x : greastestCommonDivisor(y, x%y);
}
string gcdOfStrings(string str1, string str2) {
int l1 = str1.size(), l2 = str2.size();
int gcd = greastestCommonDivisor(l1, l2);
string sgcd = str1.substr(0, gcd);
// regex pattern = regex("(" + sgcd + ")+");
regex pattern(("(" + sgcd + ")+"));
return regex_match(str1, pattern) && regex_match(str2, pattern) ? sgcd: "";
}
};