For two strings s and t. We say t divides s if and only $ s = 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
boths str1
and str2
.
Example:
1 | Input: str1 = "ABABC", str2 = "ABC" |
1 | Input: str1 = "ABABAB", str2 = "ABAB" |
1 | Input: str1 = "LEET", str2 = "CODE" |
Constraints:
- $ 1 len_{str1}, len_{str2} $
str1
andstr2
consist of English uppercase letters.
Solution
1 | class Solution { |
Explanation: For two strings, if they are consisted
by the same substrings (or same ingradient), their combination in
opposite order should be the same. For example, is string A is
ababab
, string b is abab
, it’s
obvious that they have the same ingradient ab
, so let’s
combine them as $ a + b $ and $ b + a $, their results should be same.
So, after we make sure that both two strings are made of the same
ingradient, we can use gcd to find their greatest
common division.
Proof: Assume str1 = nX and str2 = mX where X represents the common divisor of these two strings. Thus, the concatenated string str1 + str2 equals (n + m)X. We get substring of length gcd(len1, len2), if this length equals the size of str2, which means the size of str2 is divisible by str1, any division should be equal when divised by gcd(str1, str2). The core is that str1 + str2 = str2 + str1.
General Solution:
1 | class Solution { |