-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathHotelReviews.java
More file actions
93 lines (72 loc) · 2.15 KB
/
HotelReviews.java
File metadata and controls
93 lines (72 loc) · 2.15 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
package Trees;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* Author - archit.s
* Date - 05/11/18
* Time - 11:06 AM
*/
public class HotelReviews {
class TrieNode{
boolean isEnd;
Map<Character,TrieNode> map;
public TrieNode(){
map = new HashMap<>();
isEnd = false;
}
public void insert(TrieNode head, String s){
TrieNode cur = head;
for(int i=0;i<s.length();i++){
if(!cur.map.containsKey(s.charAt(i))){
cur.map.put(s.charAt(i), new TrieNode());
}
cur = cur.map.get(s.charAt(i));
}
cur.isEnd = true;
}
public boolean search(TrieNode head, String s){
if(head == null){
return false;
}
TrieNode cur = head;
for(int i=0;i<s.length();i++){
if(!cur.map.containsKey(s.charAt(i))){
return false;
}
cur = cur.map.get(s.charAt(i));
}
return cur.isEnd;
}
}
public ArrayList<Integer> solve(String A, ArrayList<String> B) {
TrieNode head = new TrieNode();
TrieNode t = new TrieNode();
String[] input = A.split("_");
for(String temp: input){
t.insert(head,temp);
}
TreeMap<Integer, ArrayList<Integer>> treeMap = new TreeMap<>();
ArrayList<Integer> result = new ArrayList<>();
for(int i=0;i<B.size();i++){
int count = 0;
input = B.get(i).split("_");
for(String temp : input){
if(t.search(head,temp)){
count++;
}
}
if(!treeMap.containsKey(count)){
treeMap.put(count, new ArrayList<>());
}
treeMap.get(count).add(i);
}
for(int j=treeMap.lastKey();j>=treeMap.firstKey();j--){
if(treeMap.containsKey(j)){
result.addAll(treeMap.get(j));
}
}
return result;
}
}