From 2dce28c4d183d5b305efca25a9a0d3b525278fa7 Mon Sep 17 00:00:00 2001 From: Rashi103 <93200197+Rashi103@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:49:58 +0530 Subject: [PATCH] Update naive_search.py naive approach reduces the time complexity --- python/algorithms/strings/naive_search.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/algorithms/strings/naive_search.py b/python/algorithms/strings/naive_search.py index 5437da61b..2a72882d6 100644 --- a/python/algorithms/strings/naive_search.py +++ b/python/algorithms/strings/naive_search.py @@ -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) @@ -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 @@ -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 \ No newline at end of file + assert res_for == res_while