Greatest Common Divisor of Strings - Problem

Imagine you have two strings that are built by repeating smaller patterns, like DNA sequences with repeating base patterns. Your mission is to find the largest common repeating pattern that can construct both strings!

Given two strings str1 and str2, we say that string x divides a string if that string can be formed by concatenating x with itself multiple times. For example, "ab" divides "abababab" because "abababab" = "ab" + "ab" + "ab" + "ab".

Goal: Find the longest string x that divides both str1 and str2. If no such string exists, return an empty string.

Example: If str1 = "ABABAB" and str2 = "ABAB", then "AB" divides both strings, making it the greatest common divisor of strings!

Input & Output

example_1.py โ€” Python
$ Input: str1 = "ABABAB", str2 = "ABAB"
โ€บ Output: "AB"
๐Ÿ’ก Note: Both strings can be formed by repeating "AB": "ABABAB" = "AB"+"AB"+"AB" and "ABAB" = "AB"+"AB". No longer string can divide both.
example_2.py โ€” Python
$ Input: str1 = "AAAA", str2 = "AA"
โ€บ Output: "AA"
๐Ÿ’ก Note: "AA" divides both strings: "AAAA" = "AA"+"AA" and "AA" = "AA". While "A" also divides both, "AA" is longer.
example_3.py โ€” Python
$ Input: str1 = "LEET", str2 = "CODE"
โ€บ Output: ""
๐Ÿ’ก Note: There is no common pattern that can divide both "LEET" and "CODE", so we return an empty string.

Visualization

Tap to expand
๐Ÿงฌ DNA Pattern Discovery ProcessFinding the Greatest Common Divisor of Genetic SequencesStep 1: AnalysisStrand 1: ABABABStrand 2: ABABStep 2: ValidationCheck: S1+S2 = S2+S1ABABABAB โœ…Step 3: GCD Calcgcd(6, 4) = 2Pattern length: 2Step 4: ExtractPattern: "AB"Result found! ๐ŸŽฏ๐Ÿ”ฌ Scientific InsightIn genetics, common patterns indicate shared evolutionary origins.Similarly, if two strings share a divisor pattern:โ€ข Their concatenations must be commutative: str1+str2 = str2+str1โ€ข The pattern length equals gcd(len(str1), len(str2))Time Complexity: O(m + n) | Space Complexity: O(m + n)
Understanding the Visualization
1
Strand Analysis
We have two DNA strands with repeating patterns
2
Pattern Validation
Check if both strands share the same underlying structure
3
Length Calculation
Find the mathematical GCD of strand lengths
4
Pattern Extraction
Extract the common genetic sequence
Key Takeaway
๐ŸŽฏ Key Insight: Mathematical properties of string divisibility allow us to solve this elegantly - if strings share a common divisor, their concatenations are commutative, and the divisor length equals the GCD of their lengths!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m + n)

String concatenation and comparison takes O(m+n), GCD calculation is O(log(min(m,n))) which is dominated by string operations

n
2n
โœ“ Linear Growth
Space Complexity
O(m + n)

Space needed for string concatenation operations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค str1.length, str2.length โ‰ค 1000
  • str1 and str2 consist of English uppercase letters only
  • The answer is guaranteed to be unique
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
38.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen