Skip to content

Commit 3ac8b91

Browse files
committed
[DFS] Refactor solution to Word Squares
1 parent 49820df commit 3ac8b91

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

DFS/WordSquares.swift

+30-27
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,64 @@
22
* Question Link: https://leetcode.com/problems/word-squares/
33
* Primary idea: Classic Depth-first Search, fill out row by row, choose correct word with fixed prefix, only need to care which column is used
44
*
5-
* Time Complexity: O(n^n), Space Complexity: O(n)
5+
* Time Complexity: O(n^2), Space Complexity: O(n^2)
66
*
77
*/
88

99
class WordSquares {
1010
func wordSquares(_ words: [String]) -> [[String]] {
11-
var paths = [[String]]()
12-
13-
guard words.count > 0 else {
14-
return paths
11+
guard let first = words.first else {
12+
return [[String]]()
1513
}
1614

17-
var prefixWords = initPrefix(words), path = [String]()
15+
let prefixesToWords = buildMaps(words), rowNum = first.count
16+
var paths = [[String]](), path = [String]()
1817

19-
dfs(&paths, &path, prefixWords, 0, words[0].count)
18+
dfs(&paths, path, prefixesToWords, rowNum, 0)
2019

2120
return paths
2221
}
2322

24-
fileprivate func dfs(_ paths: inout [[String]], _ path: inout [String], _ prefixWords: [String:[String]], _ row: Int, _ len: Int) {
25-
if row == len {
26-
paths.append(Array(path))
23+
private func dfs(_ paths: inout [[String]], _ path: [String], _ prefixesToWords: [String: [String]], _ rowNum: Int, _ currentRow: Int) {
24+
if currentRow == rowNum {
25+
paths.append(path)
2726
return
2827
}
2928

30-
var pre = ""
31-
for i in 0..<row {
32-
pre = "\(pre)\(Array(path[i])[row])"
29+
var prefix = ""
30+
for i in 0..<currentRow {
31+
prefix.append(Array(path[i])[currentRow])
3332
}
3433

35-
guard let words = prefixWords[pre] else {
34+
guard let words = prefixesToWords[prefix] else {
3635
return
3736
}
3837

3938
for word in words {
40-
path.append(word)
41-
dfs(&paths, &path, prefixWords, row + 1, len)
42-
path.removeLast()
39+
dfs(&paths, path + [word], prefixesToWords, rowNum, currentRow + 1)
4340
}
4441
}
4542

46-
fileprivate func initPrefix(_ words: [String]) -> [String: [String]]{
47-
var prefix = [String: [String]]()
43+
private func buildMaps(_ words: [String]) -> [String: [String]] {
44+
var res = [String: [String]]()
4845

4946
for word in words {
50-
prefix[""] = prefix["", default:[]] + [word]
51-
52-
var pre = ""
53-
for c in word {
54-
pre = "\(pre)\(c)"
55-
56-
prefix[pre] = prefix[pre, default:[]] + [word]
47+
for prefix in prefixes(word) {
48+
res[prefix, default: []].append(word)
5749
}
5850
}
5951

60-
return prefix
52+
return res
53+
}
54+
55+
private func prefixes(_ word: String) -> [String] {
56+
var res = [""], prefix = ""
57+
58+
for char in word {
59+
prefix.append(char)
60+
res.append(prefix)
61+
}
62+
63+
return res
6164
}
6265
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| [Swift](./DFS/PartitionKEqualSumSubsets.swift)| Medium| O(k^n)| O(n)|
294294
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n^n))| O(n^2)|
295295
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n^n))| O(n)|
296-
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n^n))| O(n)|
296+
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n^2))| O(n^2)|
297297
[Word Pattern II](https://leetcode.com/problems/word-pattern-ii/)| [Swift](./DFS/WordPatternII.swift)| Hard| O(n^n)| O(n)|
298298
[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)| [Swift](./DFS/SudokuSolver.swift)| Hard| O(n^4)| O(1)|
299299
[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Swift](./DFS/RemoveInvalidParentheses.swift)| Hard| O(n^n)| O(n)|

0 commit comments

Comments
 (0)