Skip to content

Commit 4c5a12e

Browse files
authored
Create e1071.py
1 parent 9d52c93 commit 4c5a12e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

my-submissions/e1071.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def gcdOfStrings(self, str1: str, str2: str) -> str:
3+
tryLen = 0
4+
5+
# set str1 as shorter
6+
if len(str1) > len(str2) :
7+
str1, str2 = str2, str1
8+
9+
while tryLen < len(str1) and str1[tryLen] == str2[tryLen] :
10+
tryLen += 1
11+
12+
# len of x
13+
for i in range(tryLen, -1, -1) :
14+
if not i :
15+
break
16+
17+
if len(str1) % i or len(str2) % i :
18+
continue
19+
20+
if any(str1[j] != str1[j % i] or
21+
str2[j] != str1[j % i] for j in range(i, len(str1))) :
22+
continue
23+
24+
if any(str2[j] != str2[j % i] for j in range(len(str1), len(str2))) :
25+
continue
26+
27+
return str1[:i]
28+
29+
return ''

0 commit comments

Comments
 (0)