The code below is a Trie implementation in C++. Kindly checkout what is the problem in using fill(). And if any suggestions improving my implementation are welcome
Commented fill() in the TrieNode constructor function function is giving compile time error as mentioned in the snippet . Why this is behaving wierd with fill , standard for loop is doing the same job as expected.
class TrieNode {
public :
vector<TrieNode *>vec;
bool end ;
TrieNode(){
vec.resize(26); end = false;
// fill(vec.begin() , vec.end() , NULL); // did not worked
for(int i = 0; i<vec.size(); i++){
vec[i] = NULL;
}
}
bool exist(char x){
return vec[x - 'a']!=NULL;
}
};
class Trie{
public :
TrieNode * head ;
Trie(){
head = new TrieNode();
}
void insert(string st){
TrieNode * curr = head;
for(int i = 0; i<st.length() ; i++){
if(curr->exist(st[i])){
curr = curr->vec[st[i]-'a'];
}
else{
curr->vec[st[i]-'a'] = new TrieNode();
curr = curr->vec[st[i]-'a'];
}
}
curr->end = 1;
}
bool exist(string st){
TrieNode * curr = head;
for(int i = 0; i<st.length(); i++){
if(curr->exist(st[i])){
curr = curr->vec[st[i] - 'a'];
}
else{
return false;
}
}
return curr->end;
}
};
class Solution{
public :
vector<vector<int> >palindromePairs(vi& vec){
Trie tree;
tree.insert("boys");
tree.insert("boysx");
cout << tree.exist("man");
cout << tree.exist("boys") << ' ' << tree.exist("boysd");
return {{2,3} , {1 , 2}};
}
};
