AlgoMaster Logo

Greatest Common Divisor of Strings

easyFrequency5 min readUpdated June 23, 2026

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)), which connects string divisibility to integer GCD.

Matching lengths alone do not guarantee a valid GCD string. For example, "LEET" and "CODE" have gcd(4, 4) = 4, but the two strings share no characters. We need to verify that a common divisor string exists before computing it.

Key Constraints:

  • 1 <= str1.length, str2.length <= 1000 → Lengths up to 1000 leave room for an O(n^2) scan, but a linear solution is available.
  • str1 and str2 consist of uppercase English letters. → Direct character comparison is sufficient.

Approach 1: Brute Force (Check All Prefixes)

Intuition

If a string x divides both str1 and str2, then x must be a prefix of both strings. Since we want the largest such string, we can start from the longest possible candidate and work down.

The longest possible candidate cannot exceed the shorter of the two strings. Its length must also divide both len(str1) and len(str2), otherwise repeating it cannot reproduce either string exactly.

So we try every prefix of str1 whose length divides both string lengths, check whether repeating that prefix produces both strings, and return the longest prefix 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 tries many candidate lengths before settling on the answer. The next approach computes the correct length directly from the integer GCD and skips the search entirely.

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 go straight to the one candidate that could be the answer: the prefix of length gcd(len(str1), len(str2)). Then we verify that this candidate 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
1
B
2
A
3
B
4
A
5
B
str1
6
A
7
B
8
A
9
B
str2
1/4

Code