Skip to content

Commit ade1c8f

Browse files
committed
added Divisibility of Strings
1 parent af5b276 commit ade1c8f

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Languages used: Java and Python
128128

129129
### [String](./String)
130130

131+
- [Divisibility of Strings](String/DivisibilityOfStrings)
131132
- [Isomorphic Strings](String/IsomorphicStrings)
132133
- [Longest Common Prefix](String/LongestCommonPrefix)
133134
- [Minimum Steps to Make Two Strings Anagram](String/MinStepsToMakeTwoStringsAnagram)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Divisibility of Strings
2+
3+
[Leetcode Link](https://leetcode.com/discuss/general-discussion/680341/Divisibility-of-Strings)
4+
5+
![prompt](./prompt.png)
195 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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))

0 commit comments

Comments
 (0)