Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

211. Design Add and Search Words Data Structure #130

Open
jrmullen opened this issue Jun 7, 2024 · 0 comments
Open

211. Design Add and Search Words Data Structure #130

jrmullen opened this issue Jun 7, 2024 · 0 comments

Comments

@jrmullen
Copy link
Owner

jrmullen commented Jun 7, 2024

class TrieNode:
    def __init__(self):
        self.isEndOfWord = False
        self.children = {}


class WordDictionary:
    def __init__(self):
        self.root = TrieNode()

    def addWord(self, word: str) -> None:
        currentNode = self.root
        for c in word:
            if c in currentNode.children:
                currentNode = currentNode.children[c]
            else:
                # Add a new node and update the pointer
                currentNode.children[c] = TrieNode()
                currentNode = currentNode.children[c]
        # Flag the last node as the end of a word
        currentNode.isEndOfWord = True

    def search(self, word: str) -> bool:
        def dfs(k, node):
            currentNode = node

            # Iterate over all characters left in the word
            for i in range(k, len(word)):
                char = word[i]

                if char == '.':
                    # Run a DFS over each of the node's child nodes
                    for child in currentNode.children.values():
                        # If any of the children have a successful result we return True
                        if dfs(i + 1, child):
                            return True
                    return False
                else:
                    if char not in currentNode.children:
                        return False
                    # Move the pointer forward
                    currentNode = currentNode.children[char]
            # Only return true if the final node is flagged as the end of a word
            return currentNode.isEndOfWord

        # Start at the first character of the word and the root note
        return dfs(0, self.root)


# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant