forked from WenchaoD/LeetCode_Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordSearchII.swift
142 lines (109 loc) · 3.59 KB
/
WordSearchII.swift
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Question Link: https://leetcode.com/problems/word-search-ii/
* Primary idea: Classic Depth-first Search, go up, down, left, right four directions
* with the help of Trie
*
* Time Complexity: O(mn^2), m is the longest length fo a word in words
* Space Complexity: O(n^2)
*
*/
class WordSearchII {
func findWords(board: [[Character]], _ words: [String]) -> [String] {
var res = [String]()
let m = board.count
let n = board[0].count
let trie = _convertToTrie(words)
var visited = [[Bool]](count: m, repeatedValue: Array(count: n, repeatedValue: false))
for i in 0 ..< m {
for j in 0 ..< n {
_dfs(board, m, n, i, j, &visited, &res, trie, "")
}
}
return res
}
private func _dfs(board: [[Character]], _ m: Int, _ n: Int, _ i: Int, _ j: Int, inout _ visited: [[Bool]], inout _ res: [String], _ trie: Trie, _ str: String) {
// beyond matrix
guard i >= 0 && i < m && j >= 0 && j < n else {
return
}
// check visited
guard !visited[i][j] else {
return
}
// check is word prefix
let str = str + "\(board[i][j])"
guard trie.isWordPrefix(str) else {
return
}
// check word exist
if trie.isWord(str) && !res.contains(str) {
res.append(str)
}
// check four directions
visited[i][j] = true
_dfs(board, m, n, i + 1, j, &visited, &res, trie, str)
_dfs(board, m, n, i - 1, j, &visited, &res, trie, str)
_dfs(board, m, n, i, j + 1, &visited, &res, trie, str)
_dfs(board, m, n, i, j - 1, &visited, &res, trie, str)
visited[i][j] = false
}
func _convertToTrie(words: [String]) -> Trie {
let trie = Trie()
for str in words {
trie.insert(str)
}
return trie
}
class Trie {
var root: TrieNode
init() {
root = TrieNode(letter: Character("0"))
}
func insert(word: String) {
var node = root
var word = [Character](word.characters)
for i in 0 ..< word.count {
let c = word[i]
if node.children[c] == nil {
node.children[c] = TrieNode(letter: c)
}
node = node.children[c]!
}
node.isEnd = true
}
func isWord(word: String) -> Bool {
var node = root
var word = [Character](word.characters)
for i in 0 ..< word.count {
let c = word[i]
if node.children[c] == nil {
return false
}
node = node.children[c]!
}
return node.isEnd
}
func isWordPrefix(prefix: String) -> Bool {
var node = root
var prefix = [Character](prefix.characters)
for i in 0 ..< prefix.count {
let c = prefix[i]
if node.children[c] == nil {
return false
}
node = node.children[c]!
}
return true
}
}
class TrieNode {
var letter: Character
var isEnd: Bool
var children: [Character:TrieNode]
init(letter: Character) {
self.letter = letter
isEnd = false
children = [Character:TrieNode]()
}
}
}