File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 403
403
1249|[ Minimum Remove to Make Valid Parentheses] ( ./1249-minimum-remove-to-make-valid-parentheses.js ) |Medium|
404
404
1252|[ Cells with Odd Values in a Matrix] ( ./1252-cells-with-odd-values-in-a-matrix.js ) |Easy|
405
405
1267|[ Count Servers that Communicate] ( ./1267-count-servers-that-communicate.js ) |Medium|
406
+ 1268|[ Search Suggestions System] ( ./1268-search-suggestions-system.js ) |Medium|
406
407
1287|[ Element Appearing More Than 25% In Sorted Array] ( ./1287-element-appearing-more-than-25-in-sorted-array.js ) |Easy|
407
408
1290|[ Convert Binary Number in a Linked List to Integer] ( ./1290-convert-binary-number-in-a-linked-list-to-integer.js ) |Easy|
408
409
1291|[ Sequential Digits] ( ./1291-sequential-digits.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1268. Search Suggestions System
3
+ * https://leetcode.com/problems/search-suggestions-system/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given an array of strings products and a string searchWord.
7
+ *
8
+ * Design a system that suggests at most three product names from products after each
9
+ * character of searchWord is typed. Suggested products should have common prefix with
10
+ * searchWord. If there are more than three products with a common prefix return the
11
+ * three lexicographically minimums products.
12
+ *
13
+ * Return a list of lists of the suggested products after each character of searchWord
14
+ * is typed.
15
+ */
16
+
17
+ /**
18
+ * @param {string[] } products
19
+ * @param {string } searchWord
20
+ * @return {string[][] }
21
+ */
22
+ var suggestedProducts = function ( products , searchWord ) {
23
+ products . sort ( ) ;
24
+ const result = new Array ( searchWord . length ) ;
25
+ for ( let i = 0 ; i < searchWord . length ; i ++ ) {
26
+ products = products . filter ( ( word ) => word [ i ] === searchWord [ i ] ) ;
27
+ result [ i ] = products . slice ( 0 , 3 ) ;
28
+ }
29
+ return result ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments