1071. Greatest Common Divisor of Strings
For two strings
sandt, we say "tdividess" if and only ifs = t + t + t + ... + t + t(i.e.,tis concatenated with itself one or more times).Given two strings
str1andstr2, return the largest stringxsuch thatxdivides bothstr1andstr2.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: ""
def gcdOfStrings(self, str1: str, str2: str) -> str:
if str1 + str2 != str2 + str1:
return ""
from math import gcd
return str1[:gcd(len(str1), len(str2))]