From 6fa5077a50a51638795f36e1144ad128cc36af9b Mon Sep 17 00:00:00 2001 From: Bhuvanachandra G Date: Tue, 2 Jul 2024 12:09:48 +0530 Subject: [PATCH] Create Greatest Common Divisor of Strings - Leetcode 1071.py --- ... Common Divisor of Strings - Leetcode 1071.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Greatest Common Divisor of Strings - Leetcode 1071.py diff --git a/Greatest Common Divisor of Strings - Leetcode 1071.py b/Greatest Common Divisor of Strings - Leetcode 1071.py new file mode 100644 index 0000000..71bddc3 --- /dev/null +++ b/Greatest Common Divisor of Strings - Leetcode 1071.py @@ -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.