Skip to content

Commit 5014622

Browse files
authored
Create 1268-search-suggestions-system.js
1 parent e37f67a commit 5014622

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1268-search-suggestions-system.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string[]} products
3+
* @param {string} searchWord
4+
* @return {string[][]}
5+
*/
6+
const suggestedProducts = function(products, searchWord) {
7+
const res = []
8+
for(let i = 0, n = searchWord.length; i < n; i++) {
9+
const tmp = [], pre = searchWord.slice(0, i + 1)
10+
for(const e of products) {
11+
if(e.startsWith(pre)) {
12+
tmp.push(e)
13+
tmp.sort((a, b) => a.localeCompare(b))
14+
if(tmp.length > 3) tmp.pop()
15+
}
16+
}
17+
res.push(tmp)
18+
}
19+
return res
20+
};

0 commit comments

Comments
 (0)