- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContact.java
More file actions
Latest commit
55 lines (52 loc) · 1.8 KB
/
Copy pathContact.java
File metadata and controls
55 lines (52 loc) · 1.8 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
importjava.io.*;
importjava.util.*;
publicclassContact{
publicstaticList<Integer> contacts(List<List<String>> queries) {
// Write your code here
HashMap<String, Integer> map = newHashMap<String, Integer>();
List<Integer> response = newArrayList<>();
for(inti=0; i < queries.size(); i++){
List<String> query = queries.get(i);
Stringaction = query.get(0);
Stringkey = query.get(1);
if( action == "add"){
for (intj = 1; j <= key.length(); j++){
Stringsub = key.substring(0, j);
if (map.get(sub) == null){
map.put(sub, 1);
} else {
map.put(sub, map.get(sub) + 1);
}
}
}elseif(action == "find"){
if (map.get(key) == null){
response.add(0);
} else {
response.add(map.get(key));
}
}
}
returnresponse;
}
publicstaticvoidmain(String[] args) throwsIOException {
List<List<String>> queries = newArrayList<>();
List<String> query1 = newArrayList<>();
query1.add("add");
query1.add("hack");
queries.add(query1);
List<String> query2 = newArrayList<>();
query2.add("add");
query2.add("hackerrank");
queries.add(query2);
List<String> query3 = newArrayList<>();
query3.add("find");
query3.add("hac");
queries.add(query3);
List<String> query4 = newArrayList<>();
query4.add("find");
query4.add("hak");
queries.add(query4);
List<Integer> result = contacts(queries);
System.out.println(result);
}
}