Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Greatest Common Divisor of Strings - Leetcode 1071.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def gcd(self, a, b):
if b == 0:
return a
return self.gcd(b, a % b)

def gcdOfStrings(self, str1, str2):
gcdString = ""

if str1 + str2 == str2 + str1:
return str1[0 : self.gcd(len(str1), len(str2))]

return gcdString

# Time Complexity: O(log(min(n, m))) -- n and m are the lengths of strings s and t respectively.
# Space Complexity: O(log(min(n, m))) -- depending on the number of recursive calls allocation on the stack.