-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1268-search-suggestions-system.js
More file actions
30 lines (29 loc) · 1004 Bytes
/
1268-search-suggestions-system.js
File metadata and controls
30 lines (29 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 1268. Search Suggestions System
* https://leetcode.com/problems/search-suggestions-system/
* Difficulty: Medium
*
* You are given an array of strings products and a string searchWord.
*
* Design a system that suggests at most three product names from products after each
* character of searchWord is typed. Suggested products should have common prefix with
* searchWord. If there are more than three products with a common prefix return the
* three lexicographically minimums products.
*
* Return a list of lists of the suggested products after each character of searchWord
* is typed.
*/
/**
* @param {string[]} products
* @param {string} searchWord
* @return {string[][]}
*/
var suggestedProducts = function(products, searchWord) {
products.sort();
const result = new Array(searchWord.length);
for (let i = 0; i < searchWord.length; i++) {
products = products.filter((word) => word[i] === searchWord[i]);
result[i] = products.slice(0, 3);
}
return result;
};