Skip to content

Bug Report for implement-prefix-tree #5539

@hirurana

Description

@hirurana

Bug Report for https://neetcode.io/problems/implement-prefix-tree

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

The test submission fails for the hashmap solution. Although the same test passes when copying the test use case.

Image Image

Here is the code used:

class Node:
    def __init__(self, children={}, is_end=False):
        self.children = children
        self.is_end = is_end


class PrefixTree:

    def __init__(self):
        self.root = Node()
        

    def insert(self, word: str) -> None:
        curr = self.root
        for ch in word:
            if ch not in curr.children:
                curr.children[ch] = Node()
            curr = curr.children[ch]
        curr.is_end = True

    def search(self, word: str) -> bool:
        curr = self.root
        for ch in word:
            if ch not in curr.children:
                return False
            curr = curr.children[ch]
        return curr.is_end
        

    def startsWith(self, prefix: str) -> bool:
        curr = self.root
        for ch in prefix:
            if ch not in curr.children:
                return False
            curr = curr.children[ch]
        return True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions