P24 · 1 second · 256 MB

String Matching

Return every starting index where `pattern` occurs in `text`, in increasing order. Overlapping hits count. If `pattern` is empty, return an empty array (we do not match the empty string). If `text` is empty and `pattern` is not, return `[]`.

function stringMatching(text, pattern) → number[]

CONSTRAINTS

  • 0 ≤ text.length ≤ 2000
  • 0 ≤ pattern.length ≤ 2000
  • Both strings contain only lowercase letters a-z
Input: text = 'ababcab', pattern = 'ab'
Output: [0, 2, 5]
Input: text = 'aaaa', pattern = 'aa'
Output: [0, 1, 2]
Overlaps are matches.
Input: text = 'abc', pattern = 'd'
Output: []
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.