Skip to content

Commit eee1ca5

Browse files
author
Partho Biswas
committed
1268_Search_Suggestions_System
1 parent bd52437 commit eee1ca5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ I have solved quite a number of problems from several topics. See the below tabl
369369
|29| **[1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold](https://tinyurl.com/uahn37k)** | [Python](https://tinyurl.com/wu6rdaw/1292_Maximum_Side_Length_of_a_Square_with_Sum_Less_than_or_Equal_to_Threshold.py), [Swift](https://tinyurl.com/wuja3c4/1292_Maximum_Side_Length_of_a_Square_with_Sum_Less_than_or_Equal_to_Threshold.swift) | [Art 1](https://tinyurl.com/t9zgbkn) | Medium | I loved this problem. Sliding window and Binary Search |
370370
|30| **[774. Minimize Max Distance to Gas Station](https://tinyurl.com/w2hznvj)** | [Python](https://tinyurl.com/wu6rdaw/774_Minimize_Max_Distance_to_Gas_Station.py), [Swift](https://tinyurl.com/wuja3c4/774_Minimize_Max_Distance_to_Gas_Station.swift) | [Vid 1](https://tinyurl.com/tjmrjgq), [Art 1](https://tinyurl.com/teqxwh8), [Art 2](https://tinyurl.com/szc6bjw), [Art 1](https://tinyurl.com/sfc5d2j) | Hard | I loved this problem. Very important |
371371
|31| **[1231. Divide Chocolate](https://tinyurl.com/rduxzj3)** | [Python](https://tinyurl.com/wu6rdaw/1231_Divide_Chocolate.py), [Swift](https://tinyurl.com/wuja3c4/1231_Divide_Chocolate.swift) | [Art 1](https://tinyurl.com/sl7w7cb), [Art 2](https://tinyurl.com/w9wbcyl), [Art 3](https://tinyurl.com/te6wq5e) | Hard | I loved this problem. Very important |
372+
|32| **[1268. Search Suggestions System](https://tinyurl.com/ybwcd5nx)** | [Python](https://tinyurl.com/wu6rdaw/1268_Search_Suggestions_System.py), [Swift](https://tinyurl.com/wuja3c4/1268_Search_Suggestions_System.swift) | [Art 1](https://tinyurl.com/yac3kvrp), [Art 2](https://tinyurl.com/y9u3g66q), [Art 3](https://tinyurl.com/yc2u3mrx) | Medium | I loved this problem. Very versatile, ca be solved in a lot of different ways |
372373

373374

374375
</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import bisect
2+
3+
# Usig binary search
4+
class Solution(object):
5+
def suggestedProducts(self, products, searchWord):
6+
"""
7+
:type products: List[str]
8+
:type searchWord: str
9+
:rtype: List[List[str]]
10+
"""
11+
products.sort()
12+
result, prefix, startIdx = [], "", 0
13+
for char in searchWord:
14+
prefix += char
15+
startIdx = bisect.bisect_left(products, prefix, startIdx)
16+
currnetSearchRes = []
17+
for product in products[startIdx: startIdx + 3]:
18+
if product.startswith(prefix):
19+
currnetSearchRes.append(product)
20+
result.append(currnetSearchRes)
21+
return result
22+
23+

0 commit comments

Comments
 (0)