Tap here to switch tabs
Problem
Ask
Submissions

Problem: Find the Index of the First Occurrence in a String

med
30 min
Explore how to apply the sliding window pattern to identify the first occurrence index of a substring in a given string. This lesson helps you understand substring search problems, constraints, and how to implement an efficient solution using Python.

Statement

Given two strings haystack and needle, return the index of the first occurrence of needle within haystack. If needle does not exist as a substring of haystack, return 1-1.

Constraints:

  • 11 \leq haystack.length, needle.length 104\leq 10^4

  • haystack and needle consist of only lowercase English characters

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Find the Index of the First Occurrence in a String

med
30 min
Explore how to apply the sliding window pattern to identify the first occurrence index of a substring in a given string. This lesson helps you understand substring search problems, constraints, and how to implement an efficient solution using Python.

Statement

Given two strings haystack and needle, return the index of the first occurrence of needle within haystack. If needle does not exist as a substring of haystack, return 1-1.

Constraints:

  • 11 \leq haystack.length, needle.length 104\leq 10^4

  • haystack and needle consist of only lowercase English characters