forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePairUsingTries
143 lines (117 loc) · 3.08 KB
/
PalindromePairUsingTries
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
import java.util.ArrayList;
class TrieNode {
char data;
boolean isTerminating;
TrieNode children[];
int childCount;
public TrieNode(char data) {
this.data = data;
isTerminating = false;
children = new TrieNode[26];
childCount = 0;
}
}
public class Trie {
private TrieNode root;
public int count;
public Trie() {
root = new TrieNode('\0');
}
private void add(TrieNode root, String word){
if(word.length() == 0){
root.isTerminating = true;
return;
}
int childIndex = word.charAt(0) - 'a';
TrieNode child = root.children[childIndex];
if(child == null) {
child = new TrieNode(word.charAt(0));
root.children[childIndex] = child;
root.childCount++;
}
add(child, word.substring(1));
}
public void add(String word){
add(root, word);
}
private boolean search(TrieNode root, String word) {
if(word.length() == 0) {
return root.isTerminating;
}
int childIndex=word.charAt(0) - 'a';
TrieNode child=root.children[childIndex];
if(child == null) {
return false;
}
return search(child,word.substring(1));
}
public boolean search(String word) {
return search(root,word);
}
private void print(TrieNode root, String word) {
if (root == null) {
return;
}
if (root.isTerminating) {
System.out.println(word);
}
for (TrieNode child : root.children) {
if (child == null) {
continue;
}
String fwd = word + child.data;
print(child, fwd);
}
}
public void print() {
print(this.root, "");
}
private String reverse(String word) {
int n = word.length()-1;
String str2 = "";
for (int i=n; i>=0; i--) {
str2 = str2+word.charAt(i);
}
return str2;
}
public boolean isPalindromePair(ArrayList<String> words) {
Trie suffixTrie = new Trie();
for (int i=0; i<words.size(); i++) {
String str = reverse(words.get(i));
String str2 = words.get(i);
for (int j=0; j<str.length(); j++) {
suffixTrie.add(str.substring(j));
}
for (int k=0; k<str2.length(); k++) {
if (suffixTrie.search(str2.substring(k))) {
return true;
}
}
}
return false;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Runner {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static ArrayList<String> takeInput() throws IOException {
ArrayList<String> words = new ArrayList<>();
int n = Integer.parseInt(br.readLine().trim());
if (n == 0) {
return words;
}
String[] listOfWords;
listOfWords = br.readLine().split("\\s");
for (int i = 0; i < n; ++i) {
words.add(listOfWords[i]);
}
return words;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Trie root = new Trie();
ArrayList<String> words = takeInput();
System.out.println(root.isPalindromePair(words));
}
}