Блог пользователя rahul_1234

Автор rahul_1234, история, 8 лет назад, По-английски

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?

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

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.