Skip to content

Commit

Permalink
31 may
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSpiridonov committed May 31, 2021
1 parent caf01c7 commit fff2edf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions Challenges/2021/May-LeetCoding-Challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ None
| ----: | --- | --- | --- |
|52.|[N-Queens II](https://leetcode.com/problems/n-queens-ii/)|~~Python~~|Hard|
|164.|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|[Python](/Hard/164.MaximumGap.py)|Hard|
|1268.|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](/Medium/1268.SearchSuggestionsSystem.py)|Medium|

## License
The code is open-source and licensed under the [MIT License](/LICENSE).
86 changes: 86 additions & 0 deletions Medium/1268.SearchSuggestionsSystem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'''
Given an array of strings products and a string searchWord.
We want to 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 the searchWord. If there are more than three
products with a common prefix return the three
lexicographically minimums products.
Return list of lists of the suggested products after each
character of searchWord is typed.
Example:
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"],
searchWord = "mouse"
Output: [
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
Explanation: products sorted lexicographically =
["mobile","moneypot","monitor","mouse","mousepad"]
After typing m and mo all products match and
we show user ["mobile","moneypot","monitor"]
After typing mou, mous and mouse the system
suggests ["mouse","mousepad"]
Example:
Input: products = ["havana"],
searchWord = "havana"
Output: [
["havana"],
["havana"],
["havana"],
["havana"],
["havana"],
["havana"]
]
Example:
Input: products = ["bags","baggage","banner","box","cloths"],
searchWord = "bags"
Output: [
["baggage","bags","banner"],
["baggage","bags","banner"],
["baggage","bags"],["bags"]
]
Example:
Input: products = ["havana"],
searchWord = "tatiana"
Output: [[],[],[],[],[],[],[]]
Constraints:
- 1 <= products.length <= 1000
- There are no repeated elements in products.
- 1 <= Σ products[i].length <= 2 * 10^4
- All characters of products[i] are lower-case
English letters.
- 1 <= searchWord.length <= 1000
- All characters of searchWord are lower-case
English letters.
'''
#Difficulty: Medium
#41 / 41 test cases passed.
#Runtime: 628 ms
#Memory Usage: 17 MB

#Runtime: 628 ms, faster than 19.59% of Python3 online submissions for Search Suggestions System.
#Memory Usage: 17 MB, less than 89.61% of Python3 online submissions for Search Suggestions System.

class Solution:
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()
result = []
for i in range(1, len(searchWord)+1):
temp = []
for word in products:
if word.startswith(searchWord[:i]):
temp.append(word)
if 3 == len(temp):
break
result.append(temp)
return result
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python solutions of LeetCode problems.
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-553%2F1665-orange)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-554%2F1665-orange)&nbsp;
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
<br><br>
Expand All @@ -23,7 +23,7 @@ In this repository provided my Python solutions of LeetCode problems.
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 23/30
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 16/31
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 17/31
<br><br>
## Solutions
*P.S. If you like this, please leave me a star.*
Expand Down Expand Up @@ -439,6 +439,7 @@ In this repository provided my Python solutions of LeetCode problems.
|1260.|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Python](/Easy/1260.Shift2DGrid.py)|Easy|`array`|
|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|
|1266.|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Python](/Easy/1266.MinimumTimeVisitingAllPoints.py)|Easy|simple counter|
|1268.|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](/Medium/1268.SearchSuggestionsSystem.py)|Medium|Brute Force|
|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||
|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`|
|1286.|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Python](/Medium/1286.IteratorforCombination.py)|Medium|`Itertools`|
Expand Down

0 comments on commit fff2edf

Please sign in to comment.