Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update naive_search.py #1729

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions python/algorithms/strings/naive_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def naive_search_while(string,sub):
for i in range(str_len-sub_len +1):
for j in range(sub_len):
if string[i+j] == sub[j]:
if j == sub_len-1:
if j == sub_len--:
return i
else:
break

#naive approach reduces the time complexity

def naive_search_for(string,sub):
str_len = len(string)
Expand All @@ -20,9 +20,9 @@ def naive_search_for(string,sub):

while i <= str_len-sub_len and j < sub_len:
if string[i+j] == sub[j]:
j += 1
j++
else:
i += 1
i++
j = 0
return i if j == sub_len else None

Expand All @@ -33,4 +33,4 @@ def naive_search_for(string,sub):

res_for = naive_search_for(string, sub)
res_while = naive_search_while(string, sub)
assert res_for == res_while
assert res_for == res_while