Good and Naively bad hashes: analysis
If you went through a number of solutions, you\'ll notice that they mostly use the same hash for a folder:\n\n\n "(" + "".join(folder + serialize(s) for s in su
If you went through a number of solutions, you\'ll notice that they mostly use the same hash for a folder:\n\n```\n "(" + "".join(folder + serialize(s) for s in subfolders) + ")"\n ```\n \n However, if you previously solved [Find duplicate subtrees](https://leetcode.com/problems/find-duplicate-subtrees/), you\'ll know that in that case, we were dealing with binary trees (rather than n-ary like here) and the serialisation for a node could look like:\n \n ```\nstr(node.val) + serialize(node.left) + serialize(node.right)\n ```\n or\n ```\n serialize(node.left) + "," + serialialize(node.right) + "," + str(node.val)\n ```\n \n or really any other combo of simply appending serialization of 2 children to the node value itself.\n\n**So what\'s different now?**\n\n \n1.  We have multiple children and we cannot trust that the input will be sorted for us. Since we are keeping subfolders/children in a hashmap, the order of keys is not guaranteed\n\nfor input like\n```\n[[\'x\'], [\'y\']      [\'x\',\'a\'], [\'x\',\'b\'], [\'x\',\'c\'],    [\'y\',\'c\'], [\'y\',\'b\'], [\'y\',\'a\']]\n```\nIf \'x\' has a serialisation of (\'abc\') and \'y\' will have (\'cba\'), then we will miss the case \n\n![image](https://assets.leetcode.com/users/images/34bea37b-aa0f-4aed-a090-120b4a6bb3e9_1656256473.0384746.png)\n\neven though the two folders are clearly duplicates\n\n![image](https://assets.leetcode.com/users/images/674870c0-43b2-40b6-bb49-ecb3f8e33eff_1656256504.152309.png)\n\n\nthat means that *as you are building* the Tree/Node/Trie, you could sort the paths list first\u044E\n\n2. Why do we add parentheses?\n\nI decided not to add them and failed on this test case:\n\n![image](https://assets.leetcode.com/users/images/05eb4588-5d08-443b-8ce3-5006e9a7f880_1656257027.8199456.png)\n\nWe can see that they are pretty different, but without parentheses, here are:\n\nserialisation of A:\n\n```\nC + serialize(C\'s children) + W + serialise(W\'s children) = C + B + W +Y = CBWY\n```\n\n\nserialisation of Z:\n\n```\nC + serialize(C\'s children)  = C + B + W + serialize(W\'s children) = C + B + W + Y = CBWY\n```\n\n\nBut in realiaty they are more like\n```\n[C->B][W->Y]\n```\nand \n```\nC->[B][W->Y]\n```\nor any other groupping you can come up with to distinguish the different subtrees.\n
['Python', 'Java']