-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeedleInHaystack.py
More file actions
28 lines (27 loc) · 818 Bytes
/
NeedleInHaystack.py
File metadata and controls
28 lines (27 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def strStr(haystack: str, needle: str) -> int:
n = len(haystack)
m = len(needle)
if n == 0 and m != 0: return -1
if m == 0: return 0
#create jump dictionary for mismatch scenario
jump = {}
for c in range(m):
jump[needle[c]] = c
i = m - 1
k = m - 1
while i < n:
if haystack[i] == needle[k]:
if k == 0:
return i
else:
k -= 1
i -= 1
else:
j = jump.get(haystack[i], -1) #-1 if not found
i += m - min(k, j+1) #k is at index less than j, jump m-k. otherwise at m - j+1
k = m - 1 #initialize k back to m-1
return -1
if __name__ == "__main__":
print(strStr('mississippi','issi'))
print(strStr('hello','ll'))
print(strStr('aaaaa','bba'))