Given a puzzle of letters/ characters e.g.
a e r o p s
b h a r l s
w r i s l o
a s n k t q
Write a function to which this puzzle and a word will be passed to test whether that word exists in the puzzle or not.
e.g. rain and slow will return true. rain is present in the second column and slow in the third row wrapped around.
Find the signature (sorting by alphabet) for each row and each column, put them on the hashtable where the key is signature and the value is corresponding row or column O(n*n)
Get signature of input string and look up the hashtable O(1)
After we locate the row or column, we can do the string match on the row/column with input string O(n)
Feb 11, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment