Assume that all characters of pattern are different, in this case we can slide the pattern by more than 1 when a mismatch occurs.
- All characters of pattern are different.
- Slide by j.
def optimized_naïve_search(string, pattern):
n = len(string)
m = len(pattern)
i = 0
while i <= (n - m):
for j in range(m):
if string[i + j] != pattern[j]:
break
j += 1
if j == m:
print("pattern found at index: ", i)
i = i + m
elif j == 0:
i += 1
else:
i = i + j
string = "abeabc"
pattern = "abc"
optimized_naïve_search(string, pattern)
Top comments (0)