Problem ยท Trie

Suggested Products

Learn this problem
โ— EasySquare PointOA

Problem statement

There is a arr of string products given to you, and a string search. On inserting each character of search in the search bar, the program shows all the product in the products vector of which the prefix is matching. Of the products matching we need to show at most top 3 lexicographically smallest strings. All the products are unique.

Function

suggestedProducts(products: String[], search: String) โ†’ String[][]

Examples

Example 1

products = ["abcd", "abba", "adbc", "abcc"]search = "abcd"return = [["abba", "abcc", "abcd"], ["abba", "abcc", "abcd"], ["abcc", "abcd"], ["abcd"]]

The function should return a list of lists of strings, where each list corresponds to the top 3 lexicographically smallest strings in products that match the prefix formed by the search word up to that point.

Step 1: Word = "a", Matching Prefix Products = ["abba", "abcc", "abcd"]
Step 2: Word = "ab", Matching Prefix Products = ["abba", "abcc", "abcd"]
Step 3: Word = "abc", Matching Prefix Products = ["abcc", "abcd"]
Step 4: Word = "abcd", Matching Prefix Products = ["abcd"]

Constraints

๐Ÿ‹๐Ÿ‹

More Square Point problems

drafts saved locally
public String[][] suggestedProducts(String[] products, String searchWord) {
  // write your code here
}
products["abcd", "abba", "adbc", "abcc"]
search"abcd"
expected[["abba", "abcc", "abcd", "abba", "abcc", "abcd", "abcc", "abcd", "abcd"]]
checking account