Search Suggestions System

Medium Topics Company Tags

After each character of searchWord is typed, suggest up to three names from products that begin with the entire prefix typed so far.

Choose the matching names that come first in lexicographical order, and list them in that order. Return one list for every typed character, including empty lists when no product matches. Product names are unique and contain only lowercase English letters.

Example 1:

Input: products = ["cat","camera","car","dog"]
searchWord = "cat"

Output: [["camera","car","cat"],["camera","car","cat"],["cat"]]

Explanation: After "c" and "ca", the matching names in lexicographical order are "camera", "car", and "cat". After "cat", only "cat" still matches.

Example 2:

Input: products = ["a","ab","abc"]
searchWord = "abcd"

Output: [["a","ab","abc"],["ab","abc"],["abc"],[]]

Explanation: Typing "a" matches all three products. Typing "ab" leaves "ab" and "abc", then typing "abc" leaves only "abc". No product begins with "abcd", so the last list is empty.

Example 3:

Input: products = ["zebra","apple"]
searchWord = "mo"

Output: [[],[]]

Explanation: No product starts with "m", so there are no suggestions after either "m" or "mo". Return an empty list for each character typed.

Constraints:

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • The sum of all product-name lengths is at most 20000.
  • All product names are unique.
  • 1 <= searchWord.length <= 1000
  • All strings contain only lowercase English letters.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.