This is one of those problems where the clean idea matters more than the amount of code. For 1071. Greatest Common Divisor of Strings, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are greatestCommonDivisor, gcdOfStrings, greastestCommonDivisor.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 91.98% of C++ online submissions for Greatest Common Divisor of Strings.
02//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Greatest Common Divisor of Strings.
03
04class Solution {
05public:
06 int greatestCommonDivisor(int x, int y){
07 if(x < y){
08 int tmp = x;
09 x = y;
10 y = tmp;
11 }
12
13 while(y != 0){
14 int org_y = y;
15 y = x % y;
16 x = org_y;
17 }
18
19 return x;
20 }
21 string gcdOfStrings(string str1, string str2) {
22 int l1 = str1.size(), l2 = str2.size();
23 int gcd = greatestCommonDivisor(l1, l2);
24 string sgcd = str1.substr(0, gcd);
25 // cout << sgcd << endl;
26
27 // cout << "str1" << endl;
28 for(int i = 0; i < l1/gcd; i++){
29 // cout << i*gcd << endl;
30 if(str1.substr(i*gcd, gcd) != sgcd)
31 return "";
32 }
33
34 // cout << "str2" << endl;
35 for(int i = 0; i < l2/gcd; i++){
36 // cout << i*gcd << endl;
37 if(str2.substr(i*gcd, gcd) != sgcd)
38 return "";
39 }
40
41 return sgcd;
42 }
43};
44
45//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.
46//Method 3: Regular Expression
47//Runtime: 16 ms, faster than 14.18% of C++ online submissions for Greatest Common Divisor of Strings.
48//Memory Usage: 13.8 MB, less than 100.00% of C++ online submissions for Greatest Common Divisor of Strings.
49
50class Solution {
51public:
52 int greastestCommonDivisor(int x, int y){
53 return y == 0 ? x : greastestCommonDivisor(y, x%y);
54 }
55
56 string gcdOfStrings(string str1, string str2) {
57 int l1 = str1.size(), l2 = str2.size();
58 int gcd = greastestCommonDivisor(l1, l2);
59 string sgcd = str1.substr(0, gcd);
60 // regex pattern = regex("(" + sgcd + ")+");
61 regex pattern(("(" + sgcd + ")+"));
62 return regex_match(str1, pattern) && regex_match(str2, pattern) ? sgcd: "";
63 }
64};
Cost