Implement strStr()
EasyP2Coding
Return the index of the first occurrence of needle in haystack, or -1 if needle does not occur. If needle is empty, return 0.
Example 1
- Input
- haystack = "sadbutsad", needle = "sad"
- Output
- 0
Example 2
- Input
- haystack = "leetcode", needle = "leeto"
- Output
- -1
Example 3
- Input
- haystack = "abc", needle = ""
- Output
- 0
Constraints
- 0 <= haystack.length, needle.length <= 10^4
- Both strings consist of lowercase letters.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.