-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.java
126 lines (123 loc) · 3.68 KB
/
Trie.java
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
import java.util.*;
public class Trie {
public static class Node{
Node child[] = new Node[26];
Boolean eow = false ;
int frequency;
Node(){
for(int i = 0 ; i< 26 ; i++){
child[i] = null;
}
// frequency = 1;
}
}
public static Node root = new Node();
// public static void insert(String s){
// Node curr = root;
// for(int i = 0 ; i<s.length();i++){
// int idx = s.charAt(i)-'a';
// if(curr.child[idx]==null){
// curr.child[idx] = new Node();
// }else{
// curr.child[idx].frequency++;;
// }
// curr = curr.child[idx];
// }
// curr.eow = true;
// }
// public static void shortestPrefix(Node root , String ans){
// if(root == null){
// return;
// }
// if(root.frequency == 1){
// System.out.println(ans);
// return;
// }
// for(int i = 0;i<root.child.length;i++){
// if(root.child[i]!=null){
// shortestPrefix(root.child[i], ans+(char)(i+'a'));
// }
// }
// }
public static void insert(String s){
Node curr = root;
for(int level = 0 ; level<s.length();level++){
int idx = s.charAt(level)-'a';
if(curr.child[idx]==null){
curr.child[idx] = new Node();
}
curr = curr.child[idx];
}
curr.eow = true;
}
public static boolean search(String key){
Node curr = root;
for(int level = 0 ; level<key.length();level++){
int idx = key.charAt(level)-'a';
if(curr.child[idx]==null){
return false;
}
curr = curr.child[idx];
}
return curr.eow == true;
}
public static boolean wordbreaker(String key){
if(key.length()==0){
return true;
}
for(int i = 1;i<=key.length();i++){
if(search(key.substring(0,i))&&wordbreaker(key.substring(i))){
return true;
}
}
return false;
}
public static boolean startWithPref(String pref){
Node curr = root;
for(int i = 0 ; i<pref.length();i++){
int idx = pref.charAt(i)-'a';
if(curr.child[idx] == null){
return false;
}
curr = curr.child[idx];
}
return true;
}
public static int countNode(Node root){
if(root == null){
return 0;
}
int count = 0;
for(int i = 0 ; i<root.child.length;i++){
if(root.child[i]!=null){
count+=countNode(root.child[i]);
}
}
return count+1;
}
public static String ans = "";
public static void longestPrefix(Node root, StringBuilder temp){
if(root == null){
return;
}
for(int i = 0 ; i<root.child.length;i++){
if(root.child[i]!=null&&root.child[i].eow==true){
char c = (char)(i+'a');
temp.append(c);
if(temp.length()>ans.length()){
ans = temp.toString();
}
longestPrefix(root.child[i], temp);
temp.deleteCharAt(temp.length()-1); // backtrackes
}
}
}
public static void main(String arg[]){
String arr[] = {"a" , "banana", "app", "appl", "ap", "apply", "apple"};
for(int i = 0 ; i<arr.length;i++){
insert(arr[i]);
}
longestPrefix(root,new StringBuilder(""));
System.out.println(ans);
}
}