File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed
String/DivisibilityOfStrings Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,7 @@ Languages used: Java and Python
128
128
129
129
### [ String] ( ./String )
130
130
131
+ - [ Divisibility of Strings] ( String/DivisibilityOfStrings )
131
132
- [ Isomorphic Strings] ( String/IsomorphicStrings )
132
133
- [ Longest Common Prefix] ( String/LongestCommonPrefix )
133
134
- [ Minimum Steps to Make Two Strings Anagram] ( String/MinStepsToMakeTwoStringsAnagram )
Original file line number Diff line number Diff line change
1
+ # Divisibility of Strings
2
+
3
+ [ Leetcode Link] ( https://leetcode.com/discuss/general-discussion/680341/Divisibility-of-Strings )
4
+
5
+ ![ prompt] ( ./prompt.png )
Original file line number Diff line number Diff line change
1
+ def divisible (s : str , t : str ) -> int :
2
+ # this edge case catch is optional because of problem constraints
3
+ if len (s ) < len (t ) or len (s ) == 0 or len (t ) == 0 :
4
+ return - 1
5
+ # if length is not divisible
6
+ if len (s ) % len (t ) != 0 :
7
+ return - 1
8
+ # find smallest common string of two
9
+ # bcdbcdbcdbcd[1:-1] -> cdbcdbcdbc.find(bcdbcd) -> 2
10
+ i = (t + t )[1 :- 1 ].find (t )
11
+ smallestString = t [i + 1 :] # bcdbcd[3:] -> bcd
12
+ # print(smallestString)
13
+ # check if s is also repeating smallest string
14
+ while len (t ) < len (s ):
15
+ t += t
16
+ if s == t :
17
+ return len (smallestString )
18
+ else :
19
+ return - 1
20
+
21
+
22
+ s = "bcdbcdbcdbcd"
23
+ t = "bcdbcd"
24
+ print (divisible (s , t ))
You can’t perform that action at this time.
0 commit comments