A DFS can be used to search the word. Start the search from each element having the starting character of the string. The search will check all the 8- possible directions. However once a path is chosen at the starting character (1 from the 8), the function continues in that single direction direction. if charMap[i][j] = searchStr[0] Call function searchWord(int i, int j, int offsetX, int offsetY)
offsetX and offsetY will have values (0,1)-Right (1,1)-Right-Down (1,0)-Down (-1, 1)-Down-left (0, -1)... (-1, -1)... (0, -1).. (1, -1).. The function searchWord will proceed with (i+offserX, j+offSetY). We return failure whenever any out of order character is encountered or we return success when the searchStris exhausted. The question remains... "Can we do better?" On Wednesday, June 6, 2012 7:52:06 PM UTC+5:30, ashgoel wrote: > > WAP to find a word in a 2D array. The word can be formed on > row/col/diagnal/reverse diagnal > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006652 > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/oRgLr48Ywj0J. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.