GCD of Strings: the largest piece that tiles both
We say t divides s when s is just t glued to itself some whole number of times. Given two strings, find the largest x that divides both. Press Play on each figure and leave able to explain why one gcd and one concatenation settle it — not just type the trick.
One concatenation, one gcd
The whole solution is two lines. Say them cleanly and stop:
str1 + str2 doesn't equal str2 + str1, there's no common divisor — return the empty string. Otherwise the answer is the prefix of length gcd(len1, len2): str1[0, k]. One concatenation check, one gcd — O(n + m)."That's it. The rest of this page is why those two lines are correct, and how to defend each under pressure — why a gcd, and why that one concatenation test is enough to decide whether any answer exists at all.
Brute force: every prefix length
A divisor has to be a prefix of both strings, so try each possible length from the shorter string on down, and keep the first that tiles both. Correct, and a fine safety net — but it scans the strings again for every candidate.
x must be a prefix of both strings, so the longest it could be is min(6, 4) = 4. Try candidate lengths top‑down; keep the first that tiles both.The tell: the only lengths that ever pass are the ones dividing both 6 and 4. The biggest such length is their gcd — so the loop is computing a gcd the slow way. That's the compression to reach for.
Reduce it to its lengths
Strip away the letters and ask the integer version of the question. The shape left behind is unmistakable.
Euclid computes the gcd
We don't list divisors in real code — we run Euclid: replace the larger length with (larger mod smaller) until one hits zero. The survivor is the gcd, and the answer is that-long a prefix.
A handful of cheap steps. In Ruby it collapses to one built-in: str1.length.gcd(str2.length) — then slice that many characters off the front.
The one check that makes it legal
There's a trap: the gcd-length prefix only tiles both strings when a common root actually exists. One O(n) test settles it — concatenate in both orders and compare. Flip between a case that works and one that doesn't.
"ABC".str1 + str2 == str2 + str1. If they differ, no common divisor can exist and I return the empty string. Only when they match do I take the gcd-length prefix."Why that check is enough
The concatenation test isn't a heuristic — it's a theorem. Two strings commute under concatenation if and only if they're both powers of one common root. And the proof is Euclid again, this time running on the strings themselves.
The pattern, and its siblings
Internalize one reflex: when a problem is about exact repetition or even division, restate it in terms of lengths or counts, then check whether you've just written the definition of gcd or lcm. That single move catches a whole family of problems that all rhyme.
n − failure[n] (KMP) — and it's a true period only when that length divides n. Divisibility again.The edge cases that break naive code
Four traps an interviewer will probe — each one is dispatched by the two-line solution, as long as you keep both lines.
"ABAB" / "ABCD" the lengths share gcd 4, yet "ABAB" can't tile "ABCD". The concatenation test sees they differ and returns "".LEET / CODE), return the empty string "" — explicitly. Don't let it fall through to nil.str1[0, k] and str2[0, k] are identical. Take the prefix from whichever — no need to compare them.The interview cheat-sheet
str1 + str2 != str2 + str1, return "". Otherwise return str1[0, gcd(len1, len2)]. O(n + m) time."" when there's none. The prefix of either string is the same once a root exists.str1 + str2 == str2 + str1; when they do, the biggest one is the prefix of length gcd(len1, len2). That concatenation equality is the Euclidean algorithm's promise in disguise — which is exactly why a gcd of lengths gives the answer."