-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
61 lines (61 loc) · 1.78 KB
/
Copy pathSolution.java
File metadata and controls
61 lines (61 loc) · 1.78 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
import java.util.*;
class TrieNode {
TrieNode links[];
boolean isWord;
ArrayList<String> commonPrefix;
TrieNode() {
links = new TrieNode[26];
isWord = false;
commonPrefix = new ArrayList<String>();
}
public void setEnd() {
isWord = true;
}
public boolean isEnd() {
return isWord;
}
public void put(TrieNode node,char ch) {
links[ch - 'a'] = node;
}
public TrieNode get(char ch) {
return links[ch - 'a'];
}
public void addPrefix(String word) {
commonPrefix.add(word);
}
}
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String dict[] = {"shubham","shubh","preet","preeti","ram","raman","ramay","ramsey","arya","arav","aryan","ayyan"};
TrieNode root = new TrieNode();
//Construct Trie
for(String s : dict) {
TrieNode node = root;
node.addPrefix(s);
for(char ch : s.toCharArray()) {
if(node.get(ch) == null) {
node.put(new TrieNode(),ch);
}
node = node.get(ch);
node.addPrefix(s);
}
node.setEnd();
}
TrieNode node = root;
// while(true) {
// System.out.println("Enter a Character : ");
// char ch = in.next().charAt(0);
// if(ch < 97 && ch > 122)
// break;
// if(node.get(ch) == null) {
// System.out.println("No words present to suggest !!");
// break;
// }
// node = node.get(ch);
// System.out.println(node.commonPrefix);
// }
System.out.println(node.commonPrefix);
in.close();
}
}