Greatest Common Divisor of Strings
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 \le len_{str1}, len_{str2} \le 1000 $
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 $str_1=nX$ and $str_2=mX$ where $X$ represents the common divisor of these two strings. Thus, the concatenated string $str_1+str_2$ equals $(n+m)X$. We get substring of length $\mathrm{gcd}(len_1, len_2)$, if this length equals the size of $str_2$, which means the size of $str_2$ is divisible by $str_1$, any division should be equal when divised by $\mathrm{gcd}(str_1, str_2)$. The core is that $str_1 + str_2 = str_2 + str_1$.
General Solution:
1 | class Solution { |