I was asked this question at an interview and I’m curious about the optimal answer. The question is like this: you are given an n x n board filled with letters. a gaming algorithm wants to find and list all the possible words on this board, where “a word” is defined as a string of at least 3 letters, either horizontally or vertically. what’s the most time efficient way to do this?
the “word” in this question DOES NOT need to be a real word from a dictionary. the point is to find all strings of acceptable length as FAST as possible. I couldn’t think of anything else except for the brutal force approach that traverses through all spaces on the board and find all strings starting with the letter in that space, which requires O(n^3) time. how would you guys do it?
PS. I see this question got downvoted because people don’t think there’s a better solution. This, however, is a microsoft interview question and my interviewer explicitly told me that my answer was not optimal.
Let
m(x) = max {0, x}. If we use 0-based indices, there arewords starting at position
(x,y). Horizontally, to the left those ending in column0, 1, ..., y-2and to the right those ending in columnx+2, x+3, ..., n-1. Similar for the vertical words.So at each position there start between
2*(n-3)and2*(n-2)words (inclusive).More precisely, at position
(x,y), there startn-2horizontal words if and only ify = 0ory = n-1,n-3words otherwise. That makes2*(n-2) + (n-2)*(n-3) = (n-1)*(n-2)horizontal words per row. The number of vertical words per column is the same, so altogether there arenot necessarily distinct words in the grid. Assuming a not too small alphabet, the proportion of duplicates is on average not large, so it’s impossible to have an algorithm of complexity below
O(n³).If duplicates shall not be considered, that’s it, and there remains only the low-level variations of traversing the array.
If duplicates shall be removed, and the target is to list all distinct words as efficiently as possible, the question is what data structure allows to remove the duplicates as efficiently as possible. I can’t answer that, but I think a trie would be rather efficient for that.