1071. Greatest Common Divisor of Strings - Detailed Explanation
Problem Statement
You are given two strings, str1 and str2. A string x is said to divide another string s if s can be formed by concatenating x one or more times. In other words, there exists an integer n ≥ 1 such that:
s = x repeated n times
The goal is to find the largest string x (in terms of length) that divides both str1 and str2. If there is no such string, return an empty string "".
Examples:
-
Example 1:
- Input:
str1 = "ABCABC",str2 = "ABC" - Output:
"ABC" - Explanation:
"ABC"divides"ABCABC"(it can be repeated twice) and also divides"ABC"(repeated once).
- Input:
-
Example 2:
- Input:
str1 = "ABABAB",str2 = "ABAB" - Output:
"AB" - Explanation:
"AB"divides"ABABAB"(repeated 3 times) and"ABAB"(repeated 2 times). Note that even though"ABAB"divides"ABAB", it does not divide"ABABAB".
- Input:
-
Example 3:
- Input:
str1 = "LEET",str2 = "CODE" - Output:
"" - Explanation:
There is no non-empty string that can be repeatedly concatenated to form both"LEET"and"CODE".
- Input:
Constraints:
- Both
str1andstr2consist of uppercase English letters. - The lengths of the strings are between 1 and 1000.
Hints
-
Divisibility Property:
If a stringxdivides bothstr1andstr2, then it must be a prefix of both strings. -
Concatenation Check:
A useful insight is that ifxdivides bothstr1andstr2, then the concatenationstr1 + str2must equalstr2 + str1. If this property does not hold, there is no common divisor string. -
GCD of Lengths:
The maximum possible length of the common divisor string is the greatest common divisor (GCD) of the lengths ofstr1andstr2. Once you compute this value, the candidate string is simply the prefix ofstr1(orstr2) with that length.
Approaches
Approach 1: Mathematical (Using GCD)
-
Concatenation Check:
First, check whetherstr1 + str2is equal tostr2 + str1. If they are not equal, no stringxcan form bothstr1andstr2by repeated concatenation, so return"". -
Compute GCD of Lengths:
If the above check passes, compute the greatest common divisor (gcd) of the lengths ofstr1andstr2. Let this value beg. -
Extract and Return the Divisor:
The answer is the firstgcharacters ofstr1. This substring, when repeated the necessary number of times, can reconstruct bothstr1andstr2.
Approach 2: Brute Force (Not Recommended)
- Enumerate Divisors:
Generate all possible prefixes ofstr1that are divisors (i.e., whose length divides the length ofstr1), and check whether they also dividestr2. - Drawback:
This approach is less efficient and more cumbersome compared to the mathematical method.
Step-by-Step Walkthrough (Mathematical Approach)
Let’s walk through an example:
Example:
str1 = "ABABAB", str2 = "ABAB"
-
Check Concatenation Property:
str1 + str2gives"ABABABABAB".str2 + str1gives"ABABABABAB".
Since both are equal, a common divisor string might exist.
-
Compute the GCD of Lengths:
len(str1) = 6len(str2) = 4- The greatest common divisor of 6 and 4 is 2.
-
Extract the Prefix:
- The candidate divisor string is the first 2 characters of
str1, which is"AB".
- The candidate divisor string is the first 2 characters of
-
Verification:
- Check that
"AB"repeated 3 times equals"ABABAB"and repeated 2 times equals"ABAB".
This confirms that"AB"is the greatest common divisor string.
- Check that
Code Implementations
Python Implementation
Java Implementation
Complexity Analysis
-
Time Complexity:
-
The concatenation check takes O(n + m) where n and m are the lengths of
str1andstr2. -
Computing the gcd of two numbers is O(log(min(n, m))).
-
Extracting the substring takes O(g), where g is the gcd of the lengths.
-
Overall, the approach is very efficient.
-
-
Space Complexity:
- The extra space used is O(1) (ignoring the space required to store the output).
Common Mistakes
-
Ignoring the Concatenation Check:
Failing to check ifstr1 + str2 == str2 + str1can lead to incorrect answers when the strings do not share the same repeated pattern. -
Incorrect GCD Calculation:
Make sure to correctly implement or use a library function for the gcd. -
Off-by-One Errors:
Ensure that when you extract the substring, you correctly use the gcd length.
Edge Cases
-
No Common Divisor:
For example,str1 = "LEET"andstr2 = "CODE"should return""because the concatenation property does not hold. -
One String Divides the Other:
When one string is exactly the repeated form of the other (e.g.,str1 = "ABCABC",str2 = "ABC"), the answer should be the shorter string if it divides the longer. -
Identical Strings:
Ifstr1equalsstr2, then the entire string is the answer.
Alternative Variations & Related Problems
-
Greatest Common Divisor (GCD) in Number Theory:
This problem is analogous to finding the GCD of two numbers.
-
String Pattern Matching Problems:
Problems that involve repeated patterns in strings or finding repeated substrings may use similar insights.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78