We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A single TrieNode* root, is enough!, Try to insert each character of each word of the given string vector into the tree. During the insert process as soon as you reach a leaf while you have not completely inserted current word, it means that the current word has a prefix.
This way might consume more memory but is definitely faster and have lower time complexity in compare with comparing each pair of words in the vector which would be O(n²)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
In C++, using just such a TrieNode, all 41 Test cases succeeded!
A single
TrieNode* root
, is enough!, Try to insert each character of each word of the given string vector into the tree. During the insert process as soon as you reach a leaf while you have not completely inserted current word, it means that the current word has a prefix. This way might consume more memory but is definitely faster and have lower time complexity in compare with comparing each pair of words in the vector which would be O(n²)