Greatest Common Divisor of Strings LeetCode solution in Python
1# Runtime: 85 ms (Top 15.89%) | Memory: 14 MB (Top 5.19%)2class Solution:3def gcdOfStrings(self, str1: str, str2: str) -> str:45if len(str2) > len(str1):6str1, str2 = str2, str178curr_str2 = str29while True:1011rep = len(str1) // len(curr_str2)1213if curr_str2 * rep == str1:14return curr_str21516found = False17for i in range(len(curr_str2) - 1, 1, -1):18try_str2 = curr_str2[:i]19rep2 = len(str2) // len(try_str2)2021if try_str2 * rep2 == str2:22curr_str2 = try_str223found = True24break2526if not found:27break28return ""