✅✅Java | C++  Simple Solution 🔥🔥 Easy to Understand🔥🔥
Please UPVOTE \uD83D\uDC4D\n\n!! BIG ANNOUNCEMENT !!\nI am Giving away my premium content videos related to computer science and data science and also will be s
# Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am Giving away my premium content videos related to computer science and data science and also will be sharing well-structured assignments and study materials to clear interviews at top companies to my first 1000 Subscribers. So, **DON\'T FORGET** to Subscribe\n\n**Click Here to Subscribe \uD83D\uDC49 https://www.youtube.com/@techwired8/?sub_confirmation=1**\n\n**Java:**\n```\nclass TrieNode {\n    Map<Character, TrieNode> children;\n    boolean isWord;\n    \n    public TrieNode() {\n        children = new HashMap<>();\n        isWord = false;\n    }\n}\n\nclass WordDictionary {\n    private TrieNode root;\n\n    public WordDictionary() {\n        root = new TrieNode();\n    }\n\n    public void addWord(String word) {\n        TrieNode node = root;\n        // Traverse the trie for each character in the word\n        for (int i = 0; i < word.length(); i++) {\n            char c = word.charAt(i);\n            // If the current node does not have a child with character c,\n            // create a new node and add it as a child of the current node\n            if (!node.children.containsKey(c)) {\n                node.children.put(c, new TrieNode());\n            }\n            // Move to the child node corresponding to character c\n            node = node.children.get(c);\n        }\n        // Mark the last node as a word node\n        node.isWord = true;\n    }\n\n    public boolean search(String word) {\n        return searchHelper(root, word, 0);\n    }\n\n    private boolean searchHelper(TrieNode node, String word, int index) {\n        // If we have reached the end of the word,\n        // check if the current node is a word node\n        if (index == word.length()) {\n            return node.isWord;\n        }\n        char c = word.charAt(index);\n        if (c == \'.\') {\n            // If the current character is a dot, we need to check all children of the current node\n            // recursively by skipping over the dot character and moving to the next character of the word\n            for (TrieNode child : node.children.values()) {\n                if (searchHelper(child, word, index + 1)) {\n                    return true;\n                }\n            }\n            // If no child node matches the remaining characters of the word,\n            // return false\n            return false;\n        } else {\n            // If the current character is not a dot, move to the child node\n            // corresponding to that character and continue recursively\n            TrieNode child = node.children.get(c);\n            if (child == null) {\n                // If there is no child node corresponding to the current character,\n                // return false\n                return false;\n            }\n            return searchHelper(child, word, index + 1);\n        }\n    }\n}\n\n```\n\n**C++:**\n```\n\nclass TrieNode {\npublic:\n    unordered_map<char, TrieNode*> children;\n    bool isWord;\n    \n    TrieNode() {\n        isWord = false;\n    }\n};\n\nclass WordDictionary {\nprivate:\n    TrieNode* root;\n\npublic:\n    WordDictionary() {\n        root = new TrieNode();\n    }\n\n    void addWord(string word) {\n        TrieNode* node = root;\n        // Traverse the trie for each character in the word\n        for (int i = 0; i < word.length(); i++) {\n            char c = word[i];\n            // If the current node does not have a child with character c,\n            // create a new node and add it as a child of the current node\n            if (node->children.find(c) == node->children.end()) {\n                node->children[c] = new TrieNode();\n            }\n            // Move to the child node corresponding to character c\n            node = node->children[c];\n        }\n        // Mark the last node as a word node\n        node->isWord = true;\n    }\n\n    bool search(string word) {\n        return searchHelper(root, word, 0);\n    }\n\n    bool searchHelper(TrieNode* node, string& word, int index) {\n        // If we have reached the end of the word,\n        // check if the current node is a word node\n        if (index == word.length()) {\n            return node->isWord;\n        }\n        char c = word[index];\n        if (c == \'.\') {\n            // If the current character is a dot, we need to check all children of the current node\n            // recursively by skipping over the dot character and moving to the next character of the word\n            for (auto& p : node->children) {\n                if (searchHelper(p.second, word, index + 1)) {\n                    return true;\n                }\n            }\n            // If no child node matches the remaining characters of the word,\n            // return false\n            return false;\n        } else {\n            // If the current character is not a dot, move to the child node\n            // corresponding to that character and continue recursively\n            if (node->children.find(c) == node->children.end()) {\n                // If there is no child node corresponding to the current character,\n                // return false\n                return false;\n            }\n            TrieNode* child = node->children[c];\n            return searchHelper(child, word, index + 1);\n        }\n    }\n};\n\n```\n![image.png](https://assets.leetcode.com/users/images/e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png)\n\n# Please UPVOTE \uD83D\uDC4D
['String', 'Depth-First Search', 'Trie', 'C++', 'Java']