-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcdOfStrings.py
29 lines (28 loc) · 955 Bytes
/
gcdOfStrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution(object):
def gcdOfStrings(self, str1, str2):
strLen1 = len(str1)
strLen2 = len(str2)
smaller = str1
longer = str2
if strLen1 > strLen2:
smaller = str2
longer = str1
divider = 1
smallerLen = len(smaller)
longerLen = len(longer)
current = ''
while divider <= smallerLen:
if smallerLen % divider != 0 or longerLen % divider !=0:
divider += 1
continue
smallerDiv = int(smallerLen / divider)
longerDiv = int(longerLen / divider)
smallerMerge = smaller[0:divider] * smallerDiv
longerMerge = smaller[0:divider] * longerDiv
if smallerMerge == smaller and longerMerge == longer:
current = smaller[0:divider]
divider += 1
return current
sol = Solution()
res = sol.gcdOfStrings("ab", "abab")
print(res)