Your question is First Occurrence of Substring. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
TomTom navigation software may need to locate the first occurrence of a search phrase within a route instruction or map label. Given two strings, haystack and needle, return the index of the first character where needle occurs in haystack.
If needle does not occur, return -1. If needle is empty, return 0.
Implement str_str(haystack, needle):
haystack and needle.needle in haystack, or -1 if no occurrence exists.For the target complexity, preprocess needle using the Knuth-Morris-Pratt algorithm rather than restarting comparisons after every mismatch.
def str_str(haystack, needle):