Problem
Given two strings haystack and needle, return the index where needle first appears in haystack as a contiguous piece. If it never appears, return -1. Try it without the built-in search (find, indexOf).
Example 1
- Input
- haystack = "banana", needle = "ana"
- Output
- 1
Example 2
- Input
- haystack = "abcabd", needle = "abd"
- Output
- 3
Example 3
- Input
- haystack = "aaa", needle = "b"
- Output
- -1
Constraints
1 <= haystack.length, needle.length <= 10^4Both strings contain only lowercase English letters.