Skip to content

Commit 618e83d

Browse files
committed
substring search brute force
1 parent 5a57fbb commit 618e83d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

substring-search/brute_force.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
# O(m * n)
4+
# m - length of the text
5+
# n - length of pattern
6+
def search(text: str, pattern: str) -> int:
7+
8+
last_index: int = len(text) - len(pattern) + 1
9+
for t in range(last_index):
10+
p:int = 0
11+
while p < len(pattern):
12+
if text[t + p] != pattern[p]:
13+
break
14+
else:
15+
p += 1
16+
if p == len(pattern):
17+
return t
18+
t += 1
19+
return -1
20+
21+
22+

0 commit comments

Comments
 (0)