Greatest Common Divisor of Strings
GCD / Repetition Check
Asked at 6 companies
3 recent
- Microsoft
- Meta
- Amazon
- Oracle
- Bloomberg
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementFor two strings s and t, we say "t divides s" if s is made by repeating t one or more times. Given str1 and str2, return the largest string x such that x divides both str1 and str2. If no such string exists, return "".ReadHide
Problem Statement
For two strings s and t, we say "t divides s" if s is made by repeating t one or more times. Given str1 and str2, return the largest string x such that x divides both str1 and str2. If no such string exists, return "".
Added 2026-11-01 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
GCD / Repetition Check — if a string x divides both, then str1 + str2 must equal str2 + str1 (both are just x repeated). And the length of the answer must divide both lengths, so it is gcd(len(str1), len(str2)). That one check plus one gcd gives the whole answer.
Approach
Brute Force — Try Every Divisor Length, O(n · m)
for length from min(len(str1), len(str2)) down to 1:
if len(str1) % length == 0 and len(str2) % length == 0:
candidate = str1[:length]
if str1 == candidate * (len(str1) // length) and str2 == candidate * (len(str2) // length):
return candidate
return ""
Optimized — Concatenation Check + GCD, O(n + m)
if str1 + str2 != str2 + str1: return ""
g = gcd(len(str1), len(str2))
return str1[:g]
Why it works: equal concatenations mean both strings are built from the same repeating unit, and the gcd length is the longest unit that fits both.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Try every length | O(n · m) | O(n + m) | Rebuilds each candidate string |
| Concat + GCD | O(n + m) | O(n + m) | One check, one gcd |
Solution code
import math
class SolutionBruteForce:
"""O(n * m) time — try every divisor length, longest first."""
def gcdOfStrings(self, str1: str, str2: str) -> str:
def divides(s: str, t: str) -> bool:
if len(s) % len(t) != 0:
return False
return s == t * (len(s) // len(t))
for length in range(min(len(str1), len(str2)), 0, -1):
if len(str1) % length == 0 and len(str2) % length == 0:
candidate = str1[:length]
if divides(str1, candidate) and divides(str2, candidate):
return candidate
return ""
class Solution:
"""O(n + m) time — equal concatenations + gcd of the lengths."""
def gcdOfStrings(self, str1: str, str2: str) -> str:
if str1 + str2 != str2 + str1:
return ""
return str1[:math.gcd(len(str1), len(str2))]Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language