Problem Statement

trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

Implement the Trie class:

Example 1:

Input
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
Output
[null, null, true, false, true, null, true]

Explanation
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // return True
trie.search("app");     // return False
trie.startsWith("app"); // return True
trie.insert("app");
trie.search("app");     // return True

Constraints:

Problem Link

Implement Trie (Prefix Tree) - LeetCode

Reference Video

https://www.youtube.com/watch?v=xqsaAhQC6c8&ab_channel=TECHDOSE

Code

class Trie {
public:
    /** Initialize your data structure here. */
    struct TrieNode {
        char data; //stores the character (optional field)
        bool wordend; //stores whether the word has ended with the curr character or not
                      //make it int if duplicates words are allowed
        int prefixcount;//stores how many words are there starting with a particular prefix
        TrieNode *child[26];//array of pointers to child nodes
        
        TrieNode(char d)
        {
            data = d;
            wordend = false;
            prefixcount = 0;
            
            for(int i=0;i<26;i++)
                child[i] = NULL;
        }
    };
    TrieNode *root;
    Trie() {
        root = new TrieNode('/'); //initializing the root node
        
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        TrieNode *curr = root;
        
        for(char ch:word)
        {
            int index = ch-'a';
            if(!curr->child[index])
                curr->child[index] = new TrieNode(ch);
            curr->child[index]->prefixcount++;
            curr = curr->child[index];
        }
        curr->wordend = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        
        TrieNode *curr = root;
        
        for(char ch:word)
        {
            int index = ch-'a';
            if(!curr->child[index])
                return false;
            curr = curr->child[index];
        }
        return curr->wordend;
        
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        
        TrieNode *curr = root;
        
        for(char ch:prefix)
        {
            int index = ch-'a';
            if(!curr->child[index])
                return false;
            curr = curr->child[index];
        }
        return curr->prefixcount ? true : false;
        
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */