FastPrepFastPrep
Problem Brief

Suggested Products

OA

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.

1Example 1

Input
products = ["abcd", "abba", "adbc", "abcc"], search = "abcd"
Output
[["abba", "abcc", "abcd"], ["abba", "abcc", "abcd"], ["abcc", "abcd"], ["abcd"]]
Explanation

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

Limits and guarantees your solution can rely on.

๐Ÿ‹๐Ÿ‹
public String[][] suggestedProducts(String[] products, String searchWord) {
  // write your code here
}
Input

products

["abcd", "abba", "adbc", "abcc"]

search

"abcd"

Output

[["abba", "abcc", "abcd", "abba", "abcc", "abcd", "abcc", "abcd", "abcd"]]

Sign in to submit your solution.