Search Suggestions System

Given a list of product names and a search word, return up to three lexicographically smallest suggestions for every prefix of the search word. Pattern focus: Trie Node Structure. A correct solution should maintain ordering, prefix traversal, and empty-result handling.

Input Format

products = product catalog, searchWord = typed search text

Output Format

suggestions for each prefix of searchWord

Constraints

  • 1 <= products.length <= 10^4
  • 1 <= searchWord.length <= 10^3
  • Products contain only lowercase English letters and are unique.

Examples

Example 1:

Input:

products = ["mobile","mouse","moneypot","monitor","mousepad"]
searchWord = "mouse"

Output:

[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]

Explanation:

Classic trie-based suggestion list where the candidate set shrinks as the prefix grows.

Example 2:

Input:

products = ["bags","baggage","banner","box","cloths"]
searchWord = "bags"

Output:

[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]

Explanation:

A longer prefix reduces the number of matching suggestions.

Example 3:

Input:

products = ["apple","orange"]
searchWord = "zz"

Output:

[[],[]]

Explanation:

No product matches any prefix, so every suggestion list is empty.

Loading...
Search Suggestions System - Trie DSA Problem