rahul_1234's blog

By rahul_1234, history, 7 years ago, In English

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The cell itself does not count as an adjacent cell. The same letter cell may be used more than once.

1). Grid: ab

Word = abab

return 0

2). Grid

[ ["ABCE"],

["SFCS"],

["ADEE"] ]

Word = "ABCCED"

return 1

I cannot get a proper solution to this which handle all cases so if someone can help, it would be very helpful?

  • Vote: I like it
  • +14
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +6 Vote: I do not like it

I think your state should look like this — dp[row][column][position_of_current_character] (position of current character in given word so that first position_of_current_character — 1 characters match). From current state you can try all adjacent cells and check if they match next character in given word. If they match you can go to there (dp[new_row][new_column][position_of_current_character + 1]). I hope this helps.