Search in a Trie
This lesson defines all the cases we need to consider when searching for a word in a Trie. We will also look at the implementation of this algorithm in Java.
Searching for a word in Trie
If we want to search whether a word is present in the Trie or not, then we just need to keep tracing the path in the Trie that corresponds to the characters in the word.
Case 1: Word is not present in Trie
If there is no path, as with the word “bedroom” in the below example, then we will only be able to trace till “bed”. Therefore, we return false
because there is no character path for “r” after “bed”, indicating that this word is not present in the Trie.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.