-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0422_valid_word_squares.py
More file actions
39 lines (34 loc) · 1.13 KB
/
0422_valid_word_squares.py
File metadata and controls
39 lines (34 loc) · 1.13 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
import unittest
from typing import *
import collections
# tags:
# Time = O()
# Space = O(N * L) where n is number of words and L is the length of word
class Solution:
def wordSquares(self, words):
n = len(words[0])
lookup = collections.defaultdict(list)
# create a lookup table for words that begin with a prefix
for word in words:
for i in range(n):
lookup[word[:i]].append(word)
def build(square):
if len(square) == n:
result.append(square)
return
for word in lookup[''.join(list(zip(*square))[len(square)])]:
build(square + [word])
result = []
for word in words:
build([word])
return result
class TestSolution1(unittest.TestCase):
def test_simple(self):
words = ["ball","area","lead","wall","lady"]
s = Solution()
sol_set = set()
# sol_set.add(["wall", "area", "lead", "lady"])
# sol_set.add(["ball", "area", "lead", "lady"])
self.assertEqual(s.wordSquares(words), sol_set)
if __name__ == "__main__":
unittest.main()