-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchSuggestionSystem.java
More file actions
144 lines (124 loc) · 4.98 KB
/
Copy pathSearchSuggestionSystem.java
File metadata and controls
144 lines (124 loc) · 4.98 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* You are given an array of strings products and a string searchWord.
*
* Design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
*
* Return a list of lists of the suggested products after each character of searchWord is typed.
*
*
*
* Example 1:
*
* Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
* Output: [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
* Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"].
* After typing m and mo all products match and we show user ["mobile","moneypot","monitor"].
* After typing mou, mous and mouse the system suggests ["mouse","mousepad"].
* Example 2:
*
* Input: products = ["havana"], searchWord = "havana"
* Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
* Explanation: The only word "havana" will be always suggested while typing the search word.
*
*
* Constraints:
*
* 1 <= products.length <= 1000
* 1 <= products[i].length <= 3000
* 1 <= sum(products[i].length) <= 2 * 104
* All the strings of products are unique.
* products[i] consists of lowercase English letters.
* 1 <= searchWord.length <= 1000
* searchWord consists of lowercase English letters.
*/
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'getProductSuggestions' function below.
*
* The function is expected to return a 2D_STRING_ARRAY.
* The function accepts following parameters:
* 1. STRING_ARRAY products
* 2. STRING search
*/
public static List<List<String>> getProductSuggestions(List<String> pd, String search) {
// Write your code here
Collections.sort(pd);
// System.out.println(pd);
List<List<String>> ans = new ArrayList();
int st = 0, n = search.length(), m = pd.size();
String searchStr = String.valueOf(search.charAt(0));
for(int i = 0;i < n; i++){
List<String> temp = new ArrayList();
if(i > 0) searchStr += search.charAt(i);
st = searchStart(pd, searchStr, st, m - 1);
for(int k = st; k < st + 3 && k < pd.size(); k++){
if(match(pd.get(k), searchStr) == 0)
temp.add(pd.get(k));
}
ans.add(temp);
}
return ans;
}
private static int searchStart(List<String> pd, String searchStr, int st, int en){
if(st > en) return st;
int mid = (st + en) / 2;
int matchVal = match(pd.get(mid), searchStr);
// System.out.println(st + " " + en + " " + searchStr + " " + matchVal);
if(matchVal < 0) return searchStart(pd, searchStr, mid + 1, en);
else return searchStart(pd, searchStr, st, mid - 1);
}
private static int match(String a, String b){
int n = a.length(), m = b.length(), i = 0;
while(i < m){
if(i >= n) return -1;
int temp = a.charAt(i) - b.charAt(i);
if(temp == 0) i++;
else return temp;
}
return 0;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int productsCount = Integer.parseInt(bufferedReader.readLine().trim());
List<String> products = IntStream.range(0, productsCount).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
String search = bufferedReader.readLine();
List<List<String>> result = Result.getProductSuggestions(products, search);
result.stream()
.map(
r -> r.stream()
.collect(joining(" "))
)
.map(r -> r + "\n")
.collect(toList())
.forEach(e -> {
try {
bufferedWriter.write(e);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}