Problem
Given two strings, find the largest string that "divides" both โ meaning each string is formed entirely by repeating that smaller string some number of times. For instance, "AB" divides "ABABAB" because "ABABAB" = "AB" + "AB" + "AB".
Example:
- Input: str1 =
"ABABAB", str2 ="ABAB" - Output:
"AB" - Explanation: "AB" divides both strings (str1 = ABร3, str2 = ABร2)
Counter-example (no common divisor):
- Input: str1 =
"LEET", str2 ="CODE" - Output:
"" - Explanation: No string divides both, because they share no repeated structure
Intuition
If a common divisor exists, both strings are just repetitions of the same base unit. This means assembling them in either order must produce the same result: str1 + str2 must equal str2 + str1. Once that property holds, the longest such base unit has length equal to the GCD of the two string lengths โ because if str1 repeats the base m times and str2 repeats it n times, the base length divides both lengths, and the greatest such divisor gives the longest base.
Solution โ GCD of Lengths
Check whether str1 + str2 equals str2 + str1. If not, no common divisor exists. Otherwise, the answer is the prefix of str1 with length gcd(len(str1), len(str2)).
- Concatenate str1 + str2 and str2 + str1; if they differ, return
"". - Compute
common_length = gcd(len(str1), len(str2)). - Return
str1[:common_length].
1from math import gcd
2
3class Solution:
4 def gcdOfStrings(self, str1: str, str2: str) -> str:
5 # A common divisor can only exist if the strings are interchangeable by concatenation order
6 if str1 + str2 != str2 + str1:
7 return ""
8 common_length = gcd(len(str1), len(str2))
9 return str1[:common_length]Time: O(m + n) โ dominated by the string concatenation comparison, where m and n are the lengths of str1 and str2.
Space: O(m + n) โ the two concatenated strings each have length m + n.
Complexity Summary
| Approach | Time | Space | When to use |
|---|---|---|---|
| GCD of Lengths | O(m + n) | O(m + n) | Always โ the math gives a direct single-pass solution with no alternative |
Common Mistakes
- Skipping the concatenation check โ without verifying str1 + str2 == str2 + str1, you might return a wrong prefix for inputs that share no common divisor (e.g. returning "L" for "LEET" and "CODE").
- Checking prefix divisibility in a loop โ iterating through all prefix lengths and testing each one is O(nยฒ); the GCD property makes this unnecessary.
- Using gcd on character frequencies instead of string lengths โ the relevant GCD is between the repeat counts, which maps directly to string lengths, not individual character counts.
- Returning str1[:gcd_len - 1] โ the GCD length gives exactly the right slice; subtracting one yields a shorter string that may not even divide the inputs.
Related Problems
longest-common-prefixโ finds the longest shared prefix among an array of strings, a related structural comparisonvalid-anagramโ checks a string-level property by comparing character distributionsis-subsequenceโ another form of "does string A appear inside string B" with a structural constraintisomorphic-stringsโ determines whether two strings share the same repeating pattern structurefind-the-index-of-the-first-occurrence-in-a-stringโ locating a pattern string within a longer string