Here is my KMP solution in Python. It passed 73/74 test cases but failed for the long 'aaaa...' one because of a timeout.

```
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
NEXT = get_next_array(needle)
len_y = len(haystack)
len_n = len(needle)
n_index = 0
i = 0
while i < len_y and n_index < len_n:
if haystack[i] == needle[n_index]:
n_index += 1
i += 1
elif n_index > 0:
n_index = NEXT[n_index - 1] + 1
else:
i += 1
return i - len_n if n_index == len_n else -1
def get_next(N, j):
if j == 0:
return -1
i = get_next(N, j-1)
while (N[i+1] != N[j] and i >= 0):
i = get_next(N, i)
return i + 1 if N[i+1] == N[j] else -1
def get_next_array(s):
r = [0] * len(s)
for i, c in enumerate(s):
r[i] = get_next(s, i)
return r
```

Appreciate if anyone could help to point out where I missed.

Thanks a log guys!