1/22/2024
Lexicographically Smallest Equivalent String #2 attempt
There is an interesting thing happened on this attempt. In the union function, instead of assigning root1
to this.roots[root2]
, I assigned it to this.roots[index2]
and thought it was the same. But no, it is not the same. What I did was move index2 element to root1 group, not the whole group. To move the whole group, I need to assign the root, not the element. In the latter case, when findRoot traverses to the top, it will go through all the nodes to the old root, then, from the old root, it will go one step further to the new root. In the first case, if we want to find root for index2, we go from there and set the root1, and then done, which is wrong.