| 54 | """ |
| 55 | |
| 56 | def _delete(curr: TrieNode, word: str, index: int) -> bool: |
| 57 | if index == len(word): |
| 58 | # If word does not exist |
| 59 | if not curr.is_leaf: |
| 60 | return False |
| 61 | curr.is_leaf = False |
| 62 | return len(curr.nodes) == 0 |
| 63 | char = word[index] |
| 64 | char_node = curr.nodes.get(char) |
| 65 | # If char not in current trie node |
| 66 | if not char_node: |
| 67 | return False |
| 68 | # Flag to check if node can be deleted |
| 69 | delete_curr = _delete(char_node, word, index + 1) |
| 70 | if delete_curr: |
| 71 | del curr.nodes[char] |
| 72 | return len(curr.nodes) == 0 |
| 73 | return delete_curr |
| 74 | |
| 75 | _delete(self, word, 0) |
| 76 | |