Find the Index of First Match
Difficulty: Easy · Topic: Strings & pattern matching
Return the index of the first occurrence of needle in haystack, or -1 if it isn't present. If needle is the empty string, return 0. (This is the strStr / indexOf operation.)
Examples
str_str("hello", "ll")
→ 2str_str("aaaaa", "bba")
→ -1str_str("", "")
→ 0Target complexity: time O(n·m), space O(1)
Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.
Learn the idea first: the lesson for this problem.