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

Commit 0a9860e

Browse files
committed
update 1268.search-suggestions-system.0.cpp
1 parent 0d8fdad commit 0a9860e

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

‎1268.search-suggestions-system.0.cpp‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* @lc app=leetcode id=1268 lang=cpp
3+
*
4+
* [1268] Search Suggestions System
5+
*
6+
* https://leetcode.com/problems/search-suggestions-system/description/
7+
*
8+
* algorithms
9+
* Medium (58.86%)
10+
* Likes: 178
11+
* Dislikes: 62
12+
* Total Accepted: 15.5K
13+
* Total Submissions: 26.2K
14+
* Testcase Example: '["mobile","mouse","moneypot","monitor","mousepad"]\r\n"mouse"\r'
15+
*
16+
* Given an array of strings products and a string searchWord. We want to
17+
* design a system that suggests at most three product names from products
18+
* after each character of searchWord is typed. Suggested products should have
19+
* common prefix with the searchWord. If there are more than three products
20+
* with a common prefix return the three lexicographically minimums products.
21+
*
22+
* Return list of lists of the suggested products after each character of
23+
* searchWord is typed.
24+
*
25+
*
26+
* Example 1:
27+
*
28+
*
29+
* Input: products = ["mobile","mouse","moneypot","monitor","mousepad"],
30+
* searchWord = "mouse"
31+
* Output: [
32+
* ["mobile","moneypot","monitor"],
33+
* ["mobile","moneypot","monitor"],
34+
* ["mouse","mousepad"],
35+
* ["mouse","mousepad"],
36+
* ["mouse","mousepad"]
37+
* ]
38+
* Explanation: products sorted lexicographically =
39+
* ["mobile","moneypot","monitor","mouse","mousepad"]
40+
* After typing m and mo all products match and we show user
41+
* ["mobile","moneypot","monitor"]
42+
* After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
43+
*
44+
*
45+
* Example 2:
46+
*
47+
*
48+
* Input: products = ["havana"], searchWord = "havana"
49+
* Output:
50+
* [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
51+
*
52+
*
53+
* Example 3:
54+
*
55+
*
56+
* Input: products = ["bags","baggage","banner","box","cloths"], searchWord =
57+
* "bags"
58+
* Output:
59+
* [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
60+
*
61+
*
62+
* Example 4:
63+
*
64+
*
65+
* Input: products = ["havana"], searchWord = "tatiana"
66+
* Output: [[],[],[],[],[],[],[]]
67+
*
68+
*
69+
*
70+
* Constraints:
71+
*
72+
*
73+
* 1 <= products.length <= 1000
74+
* There are no repeated elements in products.
75+
* 1 <= Σ products[i].length <= 2 * 10^4
76+
* All characters of products[i] are lower-case English letters.
77+
* 1 <= searchWord.length <= 1000
78+
* All characters of searchWord are lower-case English letters.
79+
*
80+
*
81+
*/
82+
83+
// @lc code=start
84+
class Solution {
85+
public:
86+
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
87+
sort(products.begin(), products.end());
88+
89+
vector<vector<string>> ans;
90+
91+
string prefix = "";
92+
for(char &c: searchWord){
93+
prefix += c;
94+
int pos = -1;
95+
96+
for(int k = products.size(); k>=1; k/=2)
97+
while(pos+k<products.size() && products[pos+k] < prefix)
98+
pos += k;
99+
100+
vector<string> tmp;
101+
for(int j=1; j<=3 && pos+j<products.size() && products[pos+j].substr(0, prefix.size()) == prefix; j++)
102+
tmp.push_back(products[pos+j]);
103+
104+
ans.push_back(tmp);
105+
}
106+
return ans;
107+
}
108+
};
109+
// @lc code=end

0 commit comments

Comments
(0)

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