Microsoft Interview Question
Software Engineer in TestsI would use an Hash table for this problem, to initially scan the entire array into the hash, I would also track the duplicates on the hash elements , hash[x]+=1. Then I would take a string pointer in computer and go from start to finish. I will provide the code later for this problem.
The reason for using the hash is becuase in the above 8x8 array, the computer is spread in both x and y directions.
Do a dfs on the graph...for "computer"...
I would keep a track for each character, where in the matrix it occurs in a separate table. This helps me get the start node quickly. And then DFS with +1 and -1 on rows and cols as adjacent edges.
Maybe we need to be careful of backedges...maybe not...
Example: "mum" can be found by going from m to u and then back to "m". Will check if thats valid...
instead of using hash for the matrix, if we use hash of the search string the woud be gr8. so first we scan the search array in hash to see the occurance of every char in the search string then would travers the matrix one by one and indexing every occuranc of char in the search hash . if char is found that means char for search sring is found we decrement the count initially mainained. now if either at the end of inbetween all the count gets zero meansstring is found.
boolean containWord(Board board, String result, final String TOFIND){
- vodangkhoa July 03, 2008if (result.equals(TOFIND)){
return true;
} else {
foreach (Char char: board){
foreach (Char c: getNeighbors(char))
boolean result = containWord(board, result + c) || containWord(board, result);
if (result){
return true;
}
}
}
return false;
}