-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0212-word-search-ii.rb
103 lines (81 loc) · 2.43 KB
/
0212-word-search-ii.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true
# 212. Word Search II
# https://leetcode.com/problems/word-search-ii
# Hard
=begin
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must 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 in a word.
Example 1:
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Example 2:
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
Constraints:
* m == board.length
* n == board[i].length
* 1 <= m, n <= 12
* board[i][j] is a lowercase English letter.
* 1 <= words.length <= 3 * 104
* 1 <= words[i].length <= 10
* words[i] consists of lowercase English letters.
* All the strings of words are unique.
=end
# @param {Character[][]} board
# @param {String[]} words
# @return {String[]}
def find_words(board, words)
res = []
trie = {}
words.each do |word|
node = trie
word.chars.each do |char|
node[char] ||= {}
node = node[char]
end
node[:word] = word
end
(0...board.size).each do |i|
(0...board[0].size).each do |j|
res += search(board, i, j, trie)
end
end
res
end
def search(board, i, j, node)
res = []
char = board[i][j]
char_node = node[char]
return res if char_node.nil? || char_node.empty?
if char_node.key?(:word)
res << char_node[:word]
char_node.delete(:word)
if char_node.empty?
node.delete(char)
return res
end
end
board[i][j] = :visited
[[0, 1], [0, -1], [1, 0], [-1, 0]].map { |di, dj| [di + i, dj + j] }.each do |next_i, next_j|
next if next_i < 0 || next_j < 0 || next_i >= board.size || next_j >= board[0].size
next_char = board[next_i][next_j]
next if next_char == :visited
res += search(board, next_i, next_j, char_node)
if char_node.empty?
node.delete(char)
break
end
end
board[i][j] = char
res
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_find_words < Test::Unit::TestCase
def test_
assert_equal ["eat", "oath"].sort, find_words([["o", "a", "a", "n"], ["e", "t", "a", "e"], ["i", "h", "k", "r"], ["i", "f", "l", "v"]], ["oath", "pea", "eat", "rain"]).sort
assert_equal [], find_words([["a", "b"], ["c", "d"]], ["abcb"])
end
end