The power of pruning in backtracking VI
Backtracking is a powerful strategy, and yes if can become exponential even with pruning, but remember that exponential might still be acceptable depending on the size of the input. For instance, in this case there isn't a lot of pruning and the solution ends up being N^4, but N = 15 in this case which is about 50K iterations which is for all practical purposes a "linear" solution. Code is down below, cheers, ACC. Word Squares II - LeetCode You are given a string array words , consisting of distinct 4-letter strings, each containing lowercase English letters. A word square consists of 4 distinct words: top , left , right and bottom , arranged as follows: top forms the top row . bottom forms the bottom row . left forms the left column (top to bottom). right forms the right column (top to bottom). It must satisfy: top[0] == left[0] , top[3] == right[0] ...