Implement Substring Search
Problem statement
Given text and pattern, return the zero-based index of the first occurrence of pattern as a contiguous substring of text.
Return -1 when the pattern does not occur. An empty pattern returns 0, including when the text is empty.
Function
strStr(text: String, pattern: String) → intExamples
Example 1
text = "mississippi"pattern = "issip"return = 4Return the zero-based first matching position. The empty pattern occurs at index 0.
Example 2
text = "abc"pattern = "d"return = -1Return the zero-based first matching position. The empty pattern occurs at index 0.
Example 3
text = "abc"pattern = ""return = 0Return the zero-based first matching position. The empty pattern occurs at index 0.
Constraints
0 <= text.length, pattern.length <= 10^6- Both strings contain only lowercase English letters.