Leetcode 1146 Solution

This article provides solution to leetcode question 1146 (greatest-common-divisor-of-strings).

https://leetcode.com/problems/greatest-common-divisor-of-strings

Solution

class Solution:
    def gcdOfStrings(self, str1: str, str2: str) -> str:
        if len(str1) < len(str2):
            return self.gcdOfStrings(str2, str1)
        
        if len(str2) == 0:
            return str1
        
        if str1[:len(str2)] != str2:
            return ""
        
        return self.gcdOfStrings(str2, str1[len(str2):])