LC 28

Find the Index of the First Occurrence in a String

Easy · LC 1–100

stringstring-matchingtwo-pointers
Free

Candidate solution routes

Primary

KMP / prefix-function matching

Free

Invariant: Before processing the next text character, j is the maximum prefix length equal to a suffix of the processed text.

candidate confidence: 0.520 · metadata_only

Alternate 1

Binary-Search Boundary / First True

Free

Invariant: The first True always lies in [lo,hi]; indexes left of lo are proven False, while hi remains a feasible candidate.

candidate confidence: 0.488 · metadata_only

Alternate 2

Opposite-Direction Two Pointers

Free

Invariant: Every uneliminated pair uses endpoints inside [l,r]; a moved endpoint is proven unable to pair with any remaining opposite endpoint to satisfy the goal.

candidate confidence: 0.452 · metadata_only

Alternate 3

Same-Direction / Read-Write Pointers

Free

Invariant: After processing a[0:read], a[0:write] is exactly its correct stable filtered output, with write no farther than read+1.

candidate confidence: 0.434 · metadata_only

How to use this page

Read the statement and constraints first, then validate recognition signals, invariant, and complexity. Candidate score is not correctness evidence.