forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch Suggestions System.java
39 lines (38 loc) · 1.17 KB
/
Search Suggestions System.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Runtime: 68 ms (Top 48.70%) | Memory: 45.5 MB (Top 96.95%)
class Solution
{
public List<List<String>> suggestedProducts(String[] products, String searchWord)
{
PriorityQueue<String> pq= new PriorityQueue<String>();
List<List<String>> res= new LinkedList<List<String>>();
List<String> segment= new LinkedList<String>();
for(int i=0;i<products.length;i++)
pq.offer(products[i]);
for(int j=0;j<searchWord.length();j++)
{
segment= new LinkedList<String>();
pq= reduce(pq,searchWord.substring(0,j+1));
PriorityQueue<String> pri= new PriorityQueue<>(pq);
int p=0;
while(p<pq.size()&&p<3)
{
segment.add(pri.poll());
p++;
}
res.add(segment);
}
return res;
}
public PriorityQueue<String> reduce(PriorityQueue<String> pr, String filter)
{
PriorityQueue<String> p= new PriorityQueue<>();
String s="";
while(!pr.isEmpty())
{
s=pr.poll();
if(s.startsWith(filter))
p.add(s);
}
return p;
}
}