Skip to the code
cf.
+25 XP

#369 Suggestions as You Type

medium

A search box shows up to three suggestions after every keystroke. The suggestions for the text typed so far are the stored words that start with it, in alphabetical order (normal string < order), keeping only the first three. Repeated words in the list count once.

Write suggestions(words, typed) that returns an array with one entry per keystroke: entry i is the list of suggestions for typed.slice(0, i + 1). A keystroke with no matching words gets [].

Example 1

Input: words = ["modal","mount","module","monitor","mountain"], typed = "mou"
Output: [["modal","module","monitor"],["modal","module","monitor"],["mount","mountain"]]

After m and mo all five match, so the first three alphabetically are shown. After mou only two match.

Example 2

Input: words = ["tab","table"], typed = "tx"
Output: [["tab","table"],[]]

Nothing starts with tx.

Constraints

  • 1 <= words.length <= 1000
  • 1 <= typed.length <= 100
  • All strings are lowercase letters.
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).