Skip to content

Commit 6c43775

Browse files
committed
Add gcd task
1 parent 44f3eb1 commit 6c43775

File tree

1 file changed

+15
-0
lines changed
  • array-string/1071_greatest_common_divisor_of_strings

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def gcdOfStrings(self, str1: str, str2: str) -> str:
3+
length1, length2 = len(str1), len(str2)
4+
5+
def is_valid_gcd(index):
6+
if length1 % index or length2 % index:
7+
return False
8+
n1, n2 = length1 // index, length2 // index
9+
base = str1[:index]
10+
return str1 == n1 * base and str2 == n2 * base
11+
12+
for i in range(min(length1, length2), 0, -1):
13+
if is_valid_gcd(i):
14+
return str1[:i]
15+
return ""

0 commit comments

Comments
 (0)