-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTrie data structure
More file actions
191 lines (146 loc) · 4.51 KB
/
Copy pathTrie data structure
File metadata and controls
191 lines (146 loc) · 4.51 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/*
author @rahul_shetty
call add_string(String s) to insert s in the trie dictionary..
call search(String s) to check if what part of s is already present in the trie..
call delete(String s) to delete an compulsory existing string..
call print() to print the entire trie data structure
*/
public class Solution {
static class node
{
static node root = new node('.');
char c;
HashMap<Character,node> hm; //mark node with its children
boolean end;
node(char c)
{
this.c = c;
this.hm = new HashMap();
this.end = false;
}
node add(char child)
{
//if a character is already a sub child of this node,no need to create another node,return the same node
if(this.hm.containsKey(child))
return this.hm.get(child);
//else create a new node
node n = new node(child);
//add created node in this node's hashMap
this.hm.put(child,n);
//and return that node...
return n;
}
static void add_string(String s)
{
//prev stores the currently created child as it can be used as a parent for upcoming child to be created.
node prev = root;
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
prev = prev.add(c);
}
prev.end = true;
}
static void search(String s)
{
node n = root;
for(int i=0;i<s.length();i++)
{
//check if the character is present in the hashMap of its prev character's node
char c = s.charAt(i);
//if hashmap of prev character contains this character , move on
if(n.hm.containsKey(c))
n = n.hm.get(c);
else
{
tm.add(s.substring(0,i+1));
return;
}
}
}
static void delete(String s)
{
node prev = root;
Stack<node> st = new Stack();
st.push(root);
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
prev = prev.hm.get(c);
st.push(prev);
}
node k = st.pop();
k.end = false;
if(k.hm.size()>0)
{
return;
}
char prev_char = k.c;
while(true)
{
k = st.pop();
if(k.hm.size()>1 || st.isEmpty() || k.end)
{
k.hm.remove(prev_char);
return;
}
prev_char = k.c;
}
}
}
static TreeSet<String> tm = new TreeSet();
public static void main(String[] args)
{
int t = i();
nl();
String ar[] = new String[t];
int ik=0;
while(ik<t)
{
ar[ik++] = nl();
}
for(int i=0;i<t;i++)
{
if(ar[i].charAt(0) == '+') //for every unblocked site...
node.add_string(ar[i].substring(2,ar[i].length()));
}
for(int i=0;i<t;i++)
{
if(ar[i].charAt(0) == '-') //for every blocked site...
node.search(ar[i].substring(2,ar[i].length()));
}
// node.print(node.root);
System.out.println(tm.size());
for(String s : tm)
System.out.println(s);
}
public String toString()
{
System.out.print(this.c+" --> ");
for(char c : this.hm.keySet())
System.out.print(c+" ");
return "";
}
static void print(node root)
{
node n = root;
System.out.println(root);
for(node c : n.hm.values())
print(c);
}
static Scanner in = new Scanner(System.in);
static int i(){
return in.nextInt();
}
static String nl(){
return in.nextLine();
}
static String n(){
return in.next();
}
}