Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 67e0a9f

Browse files
created solution for problem71
1 parent f5916fb commit 67e0a9f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problem71/Solution.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
3+
# Sort words in ascending order
4+
products.sort()
5+
suggestions = [] # Final list of suggestions
6+
7+
# Pointers for the range of words that contain the prefix
8+
start, end = 0, len(products)-1
9+
10+
for i in range(len(searchWord)):
11+
c = searchWord[i]
12+
curr_suggestions = []
13+
14+
while (start <= end) and (len(products[start]) <= i or products[start][i] != c):
15+
start += 1
16+
17+
while (start <= end) and (len(products[end]) <= i or products[end][i] != c):
18+
end -= 1
19+
20+
# Count of words containing prefix
21+
wordCount = end - start +1
22+
23+
# Add at most 3 words to the list of current suggestions
24+
for j in range(min(3, wordCount)):
25+
curr_suggestions.append(products[start + j])
26+
27+
# Add current suggestions list to the final list of suggestions
28+
suggestions.append(curr_suggestions)
29+
30+
return suggestions

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /