[Python3] Beats 100% ✅  Working - 20.09.2024
Code\npython3 []\nclass TrieNode:\n    def __init__(self):\n        self.child = defaultdict(TrieNode)\n        self.delete = False\n    def add_word(self, word
# Code\n```python3 []\nclass TrieNode:\n    def __init__(self):\n        self.child = defaultdict(TrieNode)\n        self.delete = False\n    def add_word(self, word):\n        curr = self\n        for c in word:\n            curr = curr.child[c]\n\nclass Solution:\n    def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n        def serialize(root, seen):\n            if not root.child: return ""\n            keys = []\n            for c, child in root.child.items():\n                keys.append(c + "#" + serialize(child, seen))\n            key = "#" + "".join(sorted(keys)) + "#"\n            seen[key].append(root)\n            print(key)\n            return key\n        \n        def dfs_get_valid_path(root, path, out):\n            for c, child in root.child.items():\n                if not child.delete:\n                    new_path = path + [c]\n                    out.append(new_path)\n                    dfs_get_valid_path(child, new_path, out)\n        \n        root, seen = TrieNode(), defaultdict(list)\n        for path in paths:\n            root.add_word(path)\n        serialize(root, seen)\n        print(seen)\n\n        for nodes in seen.values():\n            if len(nodes) >= 2:\n                for node in nodes:\n                    node.delete = True\n        \n        ans = []\n        dfs_get_valid_path(root, [], ans)\n        return ans\n```\n\n![image.png](https://assets.leetcode.com/users/images/9005f775-b5a1-48d5-8a6a-9d31fb0ed86f_1726796759.6672843.png)\n![image.png](https://assets.leetcode.com/users/images/cdbcac52-b5c7-45e2-823e-87b59d33a741_1727295353.7574322.png)\n\n
['Python', 'Python3']