-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (101 loc) · 3.01 KB
/
main.go
File metadata and controls
118 lines (101 loc) · 3.01 KB
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
// Source: https://leetcode.com/problems/word-squares
// Title: Word Squares
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an array of **unique** strings `words`, return all the **word squares** (https://en.wikipedia.org/wiki/Word_square) you can build from `words`. The same word from `words` can be used **multiple times**. You can return the answer in **any order**.
//
// A sequence of strings forms a valid **word square** if the `k^th` row and column read the same string, where `0 <= k < max(numRows, numColumns)`.
//
// - For example, the word sequence `["ball","area","lead","lady"]` forms a word square because each word reads the same both horizontally and vertically.
//
// **Example 1:**
//
// ```
// Input: words = ["area","lead","wall","lady","ball"]
// Output: [["ball","area","lead","lady"],["wall","area","lead","lady"]]
// Explanation:
// The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
// ```
//
// **Example 2:**
//
// ```
// Input: words = ["abat","baba","atan","atal"]
// Output: [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]
// Explanation:
// The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
// ```
//
// **Constraints:**
//
// - `1 <= words.length <= 1000`
// - `1 <= words[i].length <= 4`
// - All `words[i]` have the same length.
// - `words[i]` consists of only lowercase English letters.
// - All `words[i]` are **unique**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"strings"
)
// Backtracking
func wordSquares(words []string) [][]string {
n := len(words[0])
var ans [][]string
rows := make([]string, n)
var backtrack func(k int)
backtrack = func(k int) {
if k == n {
square := make([]string, n)
copy(square, rows)
ans = append(ans, square)
return
}
LOOP:
for _, word := range words {
for i, row := range rows[:k] {
if row[k] != word[i] {
continue LOOP
}
}
rows[k] = word
backtrack(k + 1)
}
}
backtrack(0)
return ans
}
// Backtracking + prefix map
func wordSquares2(words []string) [][]string {
n := len(words[0])
prefixMap := make(map[string][]string)
prefixMap[""] = words
for _, word := range words {
for i := 1; i <= n; i++ {
prefixMap[word[:i]] = append(prefixMap[word[:i]], word)
}
}
var ans [][]string
rows := make([]string, n)
var backtrack func(k int)
backtrack = func(k int) {
if k == n {
square := make([]string, n)
copy(square, rows)
ans = append(ans, square)
return
}
prefix := strings.Builder{}
for _, row := range rows[:k] {
prefix.WriteByte(row[k])
}
for _, word := range prefixMap[prefix.String()] {
rows[k] = word
backtrack(k + 1)
}
}
backtrack(0)
return ans
}