For two strings s
and t
, we say "t
divides s
" if and only if s = t + t + t + ... + t + t
(i.e., t
is concatenated with itself one or more times).
Given two strings str1
and str2
, return the largest string x
such that x
divides both str1
and str2
.
Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Example 3:
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Constraints:
1 <= str1.length, str2.length <= 1000
str1
and str2
consist of English uppercase letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for finding the greatest common divisor (GCD) string involves checking every possible substring length to see if it divides both input strings evenly. We start with the shortest possible substring and gradually increase the length, verifying if it can be repeated to form the original strings. Once a divisor is found, the algorithm checks if a longer prefix exists, returning the longest of such prefixes.
Here's how the algorithm would work step-by-step:
def greatest_common_divisor_of_strings_brute_force(first_string, second_string):
shorter_string_length = min(len(first_string), len(second_string))
greatest_common_divisor = ""
for substring_length in range(1, shorter_string_length + 1):
substring = first_string[:substring_length]
# Check if the substring evenly divides the first string
if len(first_string) % substring_length == 0:
first_string_divisor_count = len(first_string) // substring_length
repeated_first_string = substring * first_string_divisor_count
if repeated_first_string == first_string:
# Check if the substring evenly divides the second string
if len(second_string) % substring_length == 0:
second_string_divisor_count = len(second_string) // substring_length
repeated_second_string = substring * second_string_divisor_count
if repeated_second_string == second_string:
greatest_common_divisor = substring
return greatest_common_divisor
This problem asks us to find the largest string that perfectly divides both input strings. We can solve this efficiently by checking if the shorter string is a repeating unit of the longer string, and then finding the greatest common divisor of their lengths.
Here's how the algorithm would work step-by-step:
def greatest_common_divisor_of_strings(string1, string2):
if string1 + string2 != string2 + string1:
return ""
# Euclidean algorithm for GCD
def gcd(length_of_string1, length_of_string2):
while(length_of_string2):
length_of_string1, length_of_string2 = length_of_string2, length_of_string1 % length_of_string2
return length_of_string1
len_string1 = len(string1)
len_string2 = len(string2)
# GCD is needed to determine length of substring
greatest_common_divisor = gcd(len_string1, len_string2)
# Return the substring from the beginning
return string1[:greatest_common_divisor]
Case | How to Handle |
---|---|
Empty string inputs (str1 = '' or str2 = '') | Return an empty string, as an empty string is considered a divisor of any string. |
One string is a prefix of the other, but not a divisor (e.g., str1 = 'ABCABC', str2 = 'ABC') | The Euclidean algorithm correctly finds the GCD in this case by iteratively reducing the larger string. |
Strings have no common divisor other than the empty string (e.g., str1 = 'ABC', str2 = 'DEF') | The Euclidean algorithm will eventually result in empty strings, which will be handled correctly. |
Extremely long strings that could cause performance issues. | The Euclidean algorithm approach is efficient, with complexity related to the string lengths not requiring excessive resources. |
str1 and str2 are identical strings. | The algorithm should return the identical string as the GCD. |
Input strings contain non-alphanumeric characters. | Assume input strings contain only alphanumeric characters as the question specifies 'strings'; handle other character types via input sanitization or validation before the core logic is executed. |
str1 or str2 is null. | Throw IllegalArgumentException or return null based on the API contract. |
One string is a multiple repetition of the other (e.g., str1 = 'ABABAB', str2 = 'AB') | The Euclidean algorithm efficiently handles this by reducing the larger string to 'AB'. |