bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 211. Design Add and Search Words Data Structure

211. Design Add and Search Words Data Structure

Leetcode Solutions

Trie-based WordDictionary

  1. Initialize the WordDictionary with a root node representing the start of the trie.
  2. Implement addWord by starting at the root and inserting each character of the word into the trie, creating new nodes as necessary. Mark the end of the word with a boolean flag.
  3. Implement search by starting at the root and traversing the trie according to the characters of the word.
    • If a normal character is encountered, move to the corresponding child node.
    • If a wildcard character is encountered, recursively search all child nodes.
    • If the end of the word is reached, check if the current node has the end-of-word flag set to true.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...