AlgoMaster Logo

Greatest Common Divisor of Strings

Last Updated: March 28, 2026

easy

Understanding the Problem

This problem asks us to find the longest string that can be repeated some number of times to form both str1 and str2. Think of it like finding the GCD of two numbers, but for strings instead.

If a string x divides both str1 and str2, then the length of x must divide both len(str1) and len(str2). This means the length of the answer is at most gcd(len(str1), len(str2)). That is the key mathematical insight connecting string divisibility to integer GCD.

But there is an important catch. Just because the lengths line up does not mean a valid GCD string exists. For example, "LEET" and "CODE" have gcd(4, 4) = 4, but the strings have completely different characters. We need a way to verify that a common divisor string actually exists before computing it.

Key Constraints:

  • 1 <= str1.length, str2.length <= 1000 → With lengths up to 1000, even O(n^2) is comfortably within limits. But we can do much better.
  • str1 and str2 consist of uppercase English letters. → No special characters to worry about. Simple character comparison is sufficient.

Approach 1: Brute Force (Check All Prefixes)

Intuition

The most natural first thought: if some string x divides both str1 and str2, then x must be a prefix of both strings. And since we want the largest such string, we can start from the longest possible candidate and work our way down.

What is the longest possible candidate? It cannot be longer than the shorter of the two strings. And its length must divide both len(str1) and len(str2), otherwise you could not repeat it to form either string exactly.

So the approach is: try every prefix of str1 whose length divides both string lengths, check if repeating that prefix produces both strings, and return the longest one that works.

Algorithm

  1. Let minLen be the minimum of len(str1) and len(str2).
  2. Iterate candidate lengths from minLen down to 1.
  3. For each candidate length len, check if len divides both len(str1) and len(str2).
  4. If so, extract the prefix candidate = str1[0..len-1].
  5. Check if repeating candidate the right number of times produces str1, and separately check for str2.
  6. If both checks pass, return candidate.
  7. If no candidate works, return "".

Example Walkthrough

1str1 = "ABCABC" (len=6), str2 = "ABC" (len=3). minLen=3
0
A
1
B
2
C
3
A
4
B
5
C
1/4

Code

This approach works but tries many unnecessary candidates. What if we could jump directly to the correct candidate length using the mathematical GCD?

Approach 2: GCD Length + Concatenation Check

Intuition

If a string x divides both str1 and str2, then the length of x must divide both len(str1) and len(str2). Since we want the longest such string, its length must be exactly gcd(len(str1), len(str2)).

So instead of trying every possible prefix, we can jump straight to the one candidate that could be the answer: the prefix of length gcd(len(str1), len(str2)). Then we just need to verify that this candidate actually divides both strings.

Algorithm

  1. Check if str1 + str2 equals str2 + str1. If not, return "".
  2. Compute gcdLen = gcd(len(str1), len(str2)).
  3. Return str1[0..gcdLen-1].

Example Walkthrough

1str1 = "ABABAB" (len=6), str2 = "ABAB" (len=4)
0
A
str1
1
B
str1
2
A
str1
3
B
str1
4
A
str1
5
B
str1
6
A
str2
7
B
str2
8
A
str2
9
B
str2
1/4

Code