Amazon Onsite Question, Grid word finder
Anonymous User
716

Dictionary[] = {"START", "NOTE","SAND","STONED"}
char[][] board =
{
{'M','S','E','F'},
{'R','A','T','D'},
{'L','O','N','E'},
{'K','A','F','B'}
};

Result = ["NOTE","SAND","STONED"]

  • Given a 4*4 grid with letters. find all valid words using below rules.
  1. No tiles can be used more than once in a single match
  2. Letters in the word must be adjacent to one another either horizontally, vertically or diagonally

Hint: optimally this solution can be solved using load dictionary into trie data structure and process matrix tile only if characters exists at root of the trie.
Brute Force: Create all possible word for each position and check if word exists in the dictionary

followup quesiton: how can you extend this to N* N matrix?

Any one who can help in python would be very helpful to solve these kind of problems.. thank you!

Comments (2)