-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotelReview.java
More file actions
121 lines (107 loc) · 3.19 KB
/
HotelReview.java
File metadata and controls
121 lines (107 loc) · 3.19 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
import java.util.Arrays;
import java.util.Comparator;
// package ADI;
class struct {
int count;
int index;
struct(int i) {
count = 0;
index = i;
}
public String toString() {
return "\ncount: " + count + "index: " + index;
}
}
class CompareCount implements Comparator<struct> {
public int compare(struct a, struct b) {
if (a.count == b.count)
return 0;
else if (a.count > b.count)
return -1;
else
return 1;
}
}
public class HotelReview {
HotelReview[] arr;
boolean isEndOfWord;
HotelReview() {
this.arr = new HotelReview[26];
for (int i = 0; i < 26; i++)
this.arr[i] = null;
this.isEndOfWord = false;
}
void insert(String str) {
HotelReview root = this;
for (int i = 0; i < str.length(); i++) {
int j = str.charAt(i) - 'a';
if (root.arr[j] == null)
root.arr[j] = new HotelReview();
root = root.arr[j];
}
root.isEndOfWord = true;
}
boolean search(String str, int index) {
if (index == str.length())
return this.isEndOfWord;
int j = str.charAt(index) - 'a';
if (this.arr[j] == null)
return false;
return this.arr[j].search(str, index + 1);
}
boolean delete(String str, int index) {
if (index == str.length()) {
if (!this.isEndOfWord)
return false;
this.isEndOfWord = true;
for (int i = 0; i < 26; i++)
if (this.arr[i] != null)
return false;
return true;
}
int j = str.charAt(index) - 'a';
if (this.arr[j] == null)
return false;
boolean var = this.arr[j].delete(str, index + 1);
if (var) {
this.arr[j] = null;
if (this.isEndOfWord)
return false;
else {
for (int i = 0; i < 26; i++)
if (this.arr[i] != null)
return false;
return true;
}
}
return true;
}
static void solution(String a, String[] b) {
String[] res = a.split("_");
HotelReview o = new HotelReview();
for (String i : res)
o.insert(i);
struct[] st = new struct[b.length];
for (int j = 0; j < b.length; j++) {
String sp[] = b[j].split("_");
st[j] = new struct(j);
for (String p : sp) {
if (o.search(p, 0))
st[j].count++;
}
}
Arrays.sort(st, new CompareCount());
for (struct i : st)
System.out.println(i.count + " " + i.index);
// System.out.println(st[2].count);
}
public static void main(String[] args) {
String a = "cool_ice_wifi";
String[] b = new String[] { "water_is_cool", "wifi", "cold_ice_drink", "cool_wifi_speed", "cool_ice_wifi_jaihind_cool" };
solution(a, b);
// HotelReview o = new HotelReview();
// o.insert("hello");
// o.insert("hell");
// System.out.println(o.search("hell", 0));
}
}