Skip to content

Commit fff2edf

Browse files
31 may
1 parent caf01c7 commit fff2edf

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

Challenges/2021/May-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ None
5959
| ----: | --- | --- | --- |
6060
|52.|[N-Queens II](https://leetcode.com/problems/n-queens-ii/)|~~Python~~|Hard|
6161
|164.|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|[Python](/Hard/164.MaximumGap.py)|Hard|
62+
|1268.|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](/Medium/1268.SearchSuggestionsSystem.py)|Medium|
6263

6364
## License
6465
The code is open-source and licensed under the [MIT License](/LICENSE).
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'''
2+
Given an array of strings products and a string searchWord.
3+
We want to design a system that suggests at most three
4+
product names from products after each character of
5+
searchWord is typed. Suggested products should have common
6+
prefix with the searchWord. If there are more than three
7+
products with a common prefix return the three
8+
lexicographically minimums products.
9+
10+
Return list of lists of the suggested products after each
11+
character of searchWord is typed.
12+
13+
Example:
14+
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"],
15+
searchWord = "mouse"
16+
Output: [
17+
["mobile","moneypot","monitor"],
18+
["mobile","moneypot","monitor"],
19+
["mouse","mousepad"],
20+
["mouse","mousepad"],
21+
["mouse","mousepad"]
22+
]
23+
Explanation: products sorted lexicographically =
24+
["mobile","moneypot","monitor","mouse","mousepad"]
25+
After typing m and mo all products match and
26+
we show user ["mobile","moneypot","monitor"]
27+
After typing mou, mous and mouse the system
28+
suggests ["mouse","mousepad"]
29+
30+
Example:
31+
Input: products = ["havana"],
32+
searchWord = "havana"
33+
Output: [
34+
["havana"],
35+
["havana"],
36+
["havana"],
37+
["havana"],
38+
["havana"],
39+
["havana"]
40+
]
41+
42+
Example:
43+
Input: products = ["bags","baggage","banner","box","cloths"],
44+
searchWord = "bags"
45+
Output: [
46+
["baggage","bags","banner"],
47+
["baggage","bags","banner"],
48+
["baggage","bags"],["bags"]
49+
]
50+
51+
Example:
52+
Input: products = ["havana"],
53+
searchWord = "tatiana"
54+
Output: [[],[],[],[],[],[],[]]
55+
56+
Constraints:
57+
- 1 <= products.length <= 1000
58+
- There are no repeated elements in products.
59+
- 1 <= Σ products[i].length <= 2 * 10^4
60+
- All characters of products[i] are lower-case
61+
English letters.
62+
- 1 <= searchWord.length <= 1000
63+
- All characters of searchWord are lower-case
64+
English letters.
65+
'''
66+
#Difficulty: Medium
67+
#41 / 41 test cases passed.
68+
#Runtime: 628 ms
69+
#Memory Usage: 17 MB
70+
71+
#Runtime: 628 ms, faster than 19.59% of Python3 online submissions for Search Suggestions System.
72+
#Memory Usage: 17 MB, less than 89.61% of Python3 online submissions for Search Suggestions System.
73+
74+
class Solution:
75+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
76+
products.sort()
77+
result = []
78+
for i in range(1, len(searchWord)+1):
79+
temp = []
80+
for word in products:
81+
if word.startswith(searchWord[:i]):
82+
temp.append(word)
83+
if 3 == len(temp):
84+
break
85+
result.append(temp)
86+
return result

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-553%2F1665-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-554%2F1665-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -23,7 +23,7 @@ In this repository provided my Python solutions of LeetCode problems.
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
2525
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 23/30
26-
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 16/31
26+
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 17/31
2727
<br><br>
2828
## Solutions
2929
*P.S. If you like this, please leave me a star.*
@@ -439,6 +439,7 @@ In this repository provided my Python solutions of LeetCode problems.
439439
|1260.|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Python](/Easy/1260.Shift2DGrid.py)|Easy|`array`|
440440
|1261.|[Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/)|[Python](/Medium/1261.FindElementsinaContaminatedBinaryTree.py)|Medium|`DFS`, iteratively|
441441
|1266.|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Python](/Easy/1266.MinimumTimeVisitingAllPoints.py)|Easy|simple counter|
442+
|1268.|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](/Medium/1268.SearchSuggestionsSystem.py)|Medium|Brute Force|
442443
|1282.|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Python](/Medium/1282.GroupthePeopleGiventheGroupSizeTheyBelongTo.py)|Medium||
443444
|1283.|[Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/)|[Python](/Medium/1283.FindtheSmallestDivisorGivenaThreshold.py)|Medium|`Binary Search`|
444445
|1286.|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Python](/Medium/1286.IteratorforCombination.py)|Medium|`Itertools`|

0 commit comments

Comments
 (0)