Given a dictionary of millions of words, give an algorithm to find the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom).
I found a very nice solution here on stackoverflow. Basically, after I construct a Trie, I use backtracking to put all possible suffixes into the matrix until I find a valid one. However, I am stuck on how to write the backtracking part.
consider I have put 'a' in my matrix and 'a' has 3 children 'b', 'c' and 'd'.
a * ? ?
* ? ? ?
? ? ? ?
? ? ? ?
So I need to put 'b' into the matrix and check 'b's children. If it doesn't work, I need to remove one 'b' and put 'c' into the matrix, and so on. So it should be like this:
a * ? ? a b * ? a b * ?
* ? ? ? -> b * ? ? (if anything doesn't work) -> c * ? ?
? ? ? ? * ? ? ? * ? ? ?
? ? ? ? ? ? ? ? ? ? ? ?
How should I implement this? Thank you.