Problem
You get two strings, str1 and str2. A string x divides a string y when y is x repeated one or more times. For example, "ab" divides "ababab".
Return the longest string that divides both str1 and str2. If no such string exists, return an empty string.
Example 1
- Input:
- str1 = "MNMNMNMN", str2 = "MNMNMN"
- Output:
- "MN"
- Explanation:
"MN"repeated 4 times givesstr1, and"MN"repeated 3 times givesstr2. No longer string divides both.
Example 2
- Input:
- str1 = "KKKKKK", str2 = "KKKK"
- Output:
- "KK"
- Explanation:
"KK"divides both strings."KKKK"dividesstr2, but it does not dividestr1.
Example 3
- Input:
- str1 = "HELLO", str2 = "WORLD"
- Output:
- ""
Constraints
- 1 <= str1.length, str2.length <= 1000
- str1 and str2 have only uppercase English letters.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 1071. Greatest Common Divisor of Strings
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source