FastPrepImplement Substring Search

Implement Substring Search

IMC Trading logoIMC Trading● MediumINTERNPHONE SCREEN
Learn

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) → int

Examples

Example 1

text = "mississippi"pattern = "issip"return = 4

Return the zero-based first matching position. The empty pattern occurs at index 0.

Example 2

text = "abc"pattern = "d"return = -1

Return the zero-based first matching position. The empty pattern occurs at index 0.

Example 3

text = "abc"pattern = ""return = 0

Return 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.

More IMC Trading problems

See IMC Trading hiring insights
public int strStr(String text, String pattern) {
    // Write your solution here.
}
text"mississippi"
pattern"issip"
expected4
Checking account…