1071. Greatest Common Divisor of Strings¶
Approach 1: Brute Force¶
- Time: $O(\min(|\texttt{str1}|, |\texttt{str2}|) \cdot (|\texttt{str1}| + |\texttt{str2}|))$
- Space: $O(|\texttt{str1}| + |\texttt{str2}|)$
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 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Approach 2: GCD¶
- Time: $O(|\texttt{str1}| + |\texttt{str2}|))$
- Space: $O(|\texttt{str1}| + |\texttt{str2}|)$
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 |
|