LC 3799 - Word Squares II
Approach This is like a backtracking problem, and we find that the size of the words is not large, so this should be the solution. We simply just try all the permutations in words[idx:end] (handling in traverse() function). This is done by swapping idx and i (from idx to end), and then call traverse() recursively. We can swap the idx and i back after the recursive call, in this case we can reuse the words order, and don’t need extra cache for the original words value (this method is commonly used in a backtracking problem). ...