Skip to the code
cf.
+25 XP

#613 Highlight Every Match

medium

A log viewer highlights every occurrence of the search term as you type. Logs can be 100,000 characters long, so the search must be fast even for terms like aaab in text like aaaa....

Write findAll(text, term) that returns every index where term starts in text, in increasing order. Matches may overlap: aa appears at 0, 1, and 2 in aaaa. Return an empty array if there are none.

Example 1

Input: text = "GET /api GET /app", term = "GET"
Output: [0,9]

GET starts at index 0 and index 9.

Example 2

Input: text = "aaaa", term = "aa"
Output: [0,1,2]

Overlapping matches all count.

Constraints

  • 0 <= text.length <= 10^5
  • 1 <= term.length <= 10^4
  • Both contain printable ASCII characters. The match is case-sensitive.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).