-
Notifications
You must be signed in to change notification settings - Fork 0
/
exist.rb
69 lines (58 loc) · 1.73 KB
/
exist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
=begin
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
=end
# @param {Character[][]} board
# @param {String} word
# @return {Boolean}
class TrieNode
attr_accessor :children, :word
def initialize
@children = {}
@word = nil
end
end
def exist(board, word)
root = build_trie(word)
board.each_with_index do |row, i|
row.each_with_index do |cell, j|
return true if dfs(board, i, j, root)
end
end
false
end
def dfs(board, i, j, node)
return false if i < 0 || i >= board.length || j < 0 || j >= board[0].length || !node.children[board[i][j]]
return true if node.children[board[i][j]].word
temp = board[i][j]
board[i][j] = '#'
node = node.children[temp]
found = dfs(board, i - 1, j, node) ||
dfs(board, i + 1, j, node) ||
dfs(board, i, j - 1, node) ||
dfs(board, i, j + 1, node)
board[i][j] = temp
node.word = nil if found
found
end
def build_trie(word)
root = TrieNode.new
node = root
word.each_char do |char|
node.children[char] ||= TrieNode.new
node = node.children[char]
end
node.word = word
root
end
board = [["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"]]
word = "AAAAAAAAAAAABAA"
puts exist(board, word) # false
=begin
Constraints:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board and word consists of only lowercase and uppercase English letters.
=end