forked from reachanihere/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PallindromPair.cpp
173 lines (134 loc) · 3.04 KB
/
PallindromPair.cpp
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
/*
Givan n number of words, you need to find if there exist any two words which can be joined to make a palindrome or any word itself is a palindrome.
Return true or false.
Input Format :
Line 1 : Integer n
Line 2 : n words (separated by space)
Output Format :
true ot false
Sample Input 1 :
4
abc def ghi cba
Sample Output 2 :
true
Sample Output 1 Expalanation :
"abc" and "cba" forms a palindrome
Sample Input 2 :
4
abc def ghi hg
Sample Output 2 :
true
Sample Output 2 Expalanation :
"ghi" and "hg" forms a palindrome
*/
#include <string>
#include <vector>
class TrieNode {
public :
char data;
TrieNode **children;
bool isTerminal;
int childCount;
TrieNode(char data) {
this -> data = data;
children = new TrieNode*[26];
for(int i = 0; i < 26; i++) {
children[i] = NULL;
}
isTerminal = false;
childCount = 0;
}
};
class Trie {
TrieNode *root;
public :
int count;
Trie() {
this->count = 0;
root = new TrieNode('\0');
}
bool insertWord(TrieNode *root, string word) {
// Base case
if(word.size() == 0) {
if (!root->isTerminal) {
root -> isTerminal = true;
return true;
} else {
return false;
}
}
// Small Calculation
int index = word[0] - 'a';
TrieNode *child;
if(root -> children[index] != NULL) {
child = root -> children[index];
}
else {
child = new TrieNode(word[0]);
root -> children[index] = child;
root->childCount++;
}
// Recursive call
return insertWord(child, word.substr(1));
}
// For user
void insertWord(string word) {
if (insertWord(root, word)) {
this->count++;
}
}
string reverse(string in){
string out="";
for(int i=in.size()-1;i>=0;i--)
out+=in[i];
return out;
}
bool search(TrieNode* root,string word){
if(word.size()==0 ){
return root->isTerminal;
}
int index=word[0]-'a';
bool ans;
if(root->children[index]!=NULL)
ans=search(root->children[index],word.substr(1));
else
ans=false;
return ans;
}
bool findIfPalindromePair(vector<string> arr) {
// Complete this function
// Return true or false.
//Store all the words first before searching it's reverse
for(int i=0;i<arr.size();i++)
this->insertWord(arr[i]);
bool ans;
for(int i=0;i<arr.size();i++){
string out=reverse(arr[i]);
ans=search(root,out) || search(root,out.substr(1)); //Pallindrom can have odd number of letters
if(ans)
break;
}
return ans;
}
};
#include <iostream>
#include <string>
using namespace std;
#include <vector>
int main() {
Trie t;
vector <string> vect;
int N;
cin >> N;
for (int i = 0; i < N; i++) {
string word;
cin >> word;
vect.push_back(word);
}
bool ans = t.findIfPalindromePair(vect);
if (ans){
cout << "true" << endl;
} else {
cout << "false" << endl;
}
}