File tree 4 files changed +54
-0
lines changed
4 files changed +54
-0
lines changed File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ '''
2
+ For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 or more times)
3
+
4
+ Return the largest string X such that X divides str1 and X divides str2.
5
+
6
+
7
+
8
+ Example 1:
9
+
10
+ Input: str1 = "ABCABC", str2 = "ABC"
11
+ Output: "ABC"
12
+ Example 2:
13
+
14
+ Input: str1 = "ABABAB", str2 = "ABAB"
15
+ Output: "AB"
16
+ Example 3:
17
+
18
+ Input: str1 = "LEET", str2 = "CODE"
19
+ Output: ""
20
+
21
+
22
+ Note:
23
+
24
+ 1 <= str1.length <= 1000
25
+ 1 <= str2.length <= 1000
26
+ str1[i] and str2[i] are English uppercase letters.
27
+
28
+ '''
29
+ class Solution (object ):
30
+ def gcdOfStrings (self , str1 , str2 ):
31
+ """
32
+ :type str1: str
33
+ :type str2: str
34
+ :rtype: str
35
+ """
36
+ if len (str1 ) > len (str2 ):
37
+ str1 , str2 = str2 , str1
38
+
39
+ l_str1 = len (str1 )
40
+ for index in range (1 , len (str1 )+ 1 ):
41
+ if l_str1 % index != 0 :
42
+ continue
43
+
44
+ size_to_take = int (l_str1 / index )
45
+ substr1 = str1 [:size_to_take ]
46
+ substr2 = str2
47
+
48
+ while substr1 == substr2 [:size_to_take ]:
49
+ substr2 = substr2 [size_to_take :]
50
+
51
+ if substr2 == "" :
52
+ return substr1
53
+ return ""
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
15
15
##### [ Problems 1000-1100] ( ./1000-1100q/ )
16
16
| # | Title | Solution | Difficulty |
17
17
| ---| ----- | -------- | ---------- |
18
+ | 1071| [ Greatest Common Divisor of Strings] ( https://leetcode.com/problems/greatest-common-divisor-of-strings ) | [ Python] ( ./1000-1100q/1071.py ) | Easy|
18
19
| 1065| [ Index Pairs of a String] ( https://leetcode.com/problems/index-pairs-of-a-string ) | [ Python] ( ./1000-1100q/1065.py ) | Easy|
19
20
| 1064| [ Fixed Point] ( https://leetcode.com/problems/fixed-point ) | [ Python] ( ./1000-1100q/1064.py ) | Easy|
20
21
| 1054| [ Distant Barcodes] ( https://leetcode.com/problems/distant-barcodes ) | [ Python] ( ./1000-1100q/1054.py ) | Medium|
You can’t perform that action at this time.
0 commit comments