📘
Implement strStr
EasyArrays & Strings
Return the index of the first occurrence of needle in haystack, or -1 if not found.
Example:
Input:
sadbutsad sad
Output:
0
Reasoning:
- The function
strStrchecks if the substringneedle("sad") exists in the stringhaystack("sadbutsad") - It starts by comparing the first character of
needlewith the first character ofhaystack, finding a match - Since the rest of the characters in
needlealso match the subsequent characters inhaystack, it confirms the presence ofneedleat the starting index - The function returns the index where the match is found, which is 0 in this case, as the
needleis found at the beginning of thehaystack
Constraints:
- 1 <= len(haystack), len(needle) <= 10^4
- Strings consist of lowercase English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.