The Runtime Theory
easyleetcode#string#two-pointers

Implement strStr

Return the index of the first occurrence of a needle in a haystack — naive substring search with O(n·m) worst case.

The Runtime Theory Team1 min read
Solve it

solving happens on the judge — come back and mark it done

Sample cases

inhaystack=hello, needle=ll

out2

inhaystack=aaaaa, needle=bba

out-1

inhaystack=, needle=

out0

Given a haystack string and a needle string, return the index of the first occurrence of the needle, or −1 if it is absent. This is the classic substring-search primitive — the machine must slide the needle across the haystack and compare character by character.

The key insight: the search window only needs to start where the needle could still fit. If the needle has length m, the last viable start is len(haystack) - m — past that, the needle is longer than the remaining text and cannot match, so the machine stops early. Each start position is a fresh comparison from the needle's first character.

Approach:

  1. Guard: an empty needle returns 0 by definition.
  2. For each start from 0 to len(haystack) - len(needle), compare the window.
  3. Return the first matching start; else −1.
python
def str_str(haystack, needle):
    n, m = len(haystack), len(needle)
    if m == 0:
        return 0
    for start in range(n - m + 1):
        if haystack[start:start + m] == needle:
            return start
    return -1

Time: O(n·m) worst case. Space: O(1).

Trickiest edge case: needle longer than haystack — range(n - m + 1) is empty and the machine returns −1, which is correct but only because of the bounds guard; without it, the slice comparison would silently fail.

More practice in this topic

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.