Skip to main content

1268 - Search Suggestions System

Details

KeyValue
Linkhttps://leetcode.com/problems/search-suggestions-system/
LanguagePython 3
Runtime568 ms, faster than 25.14% of Python3 online submissions for Search Suggestions System
Memory Usage17.1 MB, less than 55.63% of Python3 online submissions for Search Suggestions System
DatastructuresList[str]
AlgorithmsSort + Binary Search
ComplexityTime: O(NlogN+MlogN) Memory: O(logN) (N=number of elements in products, M=length of searchWord)

Procedure

  1. ...

Code

class Solution:
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()
result = []

for i in range( len(searchWord) ):
current = []
for product in products:
if product.startswith(searchWord[:i+1]):
current.append(product)

result.append(current[:3])

return result