I need to get all the unique substring of a string. I have stored the string into a trie
but I am not able to figure out how can i used the same to print all the unique substring
for example
string aab
all unique substrings are {"a", "aa", "aab", "ab", "b"}
here is my code for trie
#include <iostream>
#include <map>
#include <string>
#include <stack>
struct trie_node_t {
typedef std::map<char, trie_node_t *> child_node_t;
child_node_t m_childMap;
trie_node_t() :m_childMap(std::map<char, trie_node_t*>()) {}
void insert( std::string& word ) {
trie_node_t *pNode = this;
for ( std::string::const_iterator itr = word.begin(); itr != word.end(); ++itr) {
char letter = *itr;
if ( pNode->m_childMap.find(letter) == pNode->m_childMap.end()){
pNode->m_childMap[letter] = new trie_node_t();
}
pNode = pNode->m_childMap[letter];
}
}
void print() {
}
};
int main ( int argc, char **argv ) {
trie_node_t trie;
trie.insert(std::string("aab"));
trie.print();
}
How do i implement print
function which will print all the unique substring.
I am looking for Linear time approach
Since I have built a trie
, is there a any way I can iterate over and whenever I visit any node I can print it as a unique string.