-
Notifications
You must be signed in to change notification settings - Fork 2
/
425. Word Squares.py
150 lines (127 loc) · 3.82 KB
/
425. Word Squares.py
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
143
144
145
146
147
148
149
150
# Given a set of words (without duplicates), find all word squares you can build from them.
# A sequence of words forms a valid word square if the kth row and column read the exact 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.
# b a l l
# a r e a
# l e a d
# l a d y
# Note:
# There are at least 1 and at most 1000 words.
# All words will have the exact same length.
# Word length is at least 1 and at most 5.
# Each word contains only lowercase English alphabet a-z.
# Example 1:
# Input:
# ["area","lead","wall","lady","ball"]
# Output:
# [
# [ "wall",
# "area",
# "lead",
# "lady"
# ],
# [ "ball",
# "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:
# ["abat","baba","atan","atal"]
# Output:
# [
# [ "baba",
# "abat",
# "baba",
# "atan"
# ],
# [ "baba",
# "abat",
# "baba",
# "atal"
# ]
# ]
# 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).
# https://www.cnblogs.com/grandyang/p/6006000.html
# M1. 前缀字典
class Solution(object):
def wordSquares(self, words):
"""
:type words: List[str]
:rtype: List[List[str]]
"""
if not words:
return []
prefix_dict = {}
size = len(words[0])
for word in words:
for i in range(1, size):
key = word[:i]
if key not in prefix_dict:
prefix_dict[key] = []
prefix_dict[key].append(word)
res = []
for word in words:
sol = [word]
total_sol = []
self.helper(prefix_dict, 1, sol, size, total_sol)
if len(total_sol) != 0:
res.extend(total_sol)
return res
def helper(self, prefix_dict, index, sol, size, total_sol):
#print(index, size)
if index == size:
total_sol.append(sol)
return
prefix = ""
for w in sol:
#print(w, index)
prefix += w[index]
if prefix not in prefix_dict:
return
new_list = prefix_dict[prefix]
for word in new_list:
self.helper(prefix_dict, index + 1, sol + [word], size, total_sol)
# M2. 前缀树
class TrieNode():
def __init__(self, val):
self.val = val
self.sons = {}
self.words = []
class Solution(object):
def wordSquares(self, words):
"""
:type words: List[str]
:rtype: List[List[str]]
"""
root = TrieNode("*")
for word in words:
curr = root
for c in word:
curr.words.append(word)
if not c in curr.sons:
curr.sons[c] = TrieNode(c)
curr = curr.sons[c]
curr.words.append(word)
ans = []
def search(squares, root, n):
if len(squares) == n:
ans.append(squares)
return
l = len(squares)
curr = root
for word in squares:
if word[l] not in curr.sons:
return
curr = curr.sons[word[l]]
for word in curr.words:
#if word in squares:
# continue
search(squares + [word], root, n)
return
search([], root, len(words[0]))
return ans