Solution 1 - Concatenation Check + GCD Length
If str1+str2 != str2+str1, no common divisor string exists. Otherwise answer length is gcd(len1, len2).
""" 1071.1 - Greatest Common Divisor of Strings - Solution 1 - GCD Length """
from math import gcd
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
if str1 + str2 != str2 + str1:
return ""
return str1[:gcd(len(str1), len(str2))]