-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathword-squares-ii.py
More file actions
56 lines (51 loc) · 1.72 KB
/
word-squares-ii.py
File metadata and controls
56 lines (51 loc) · 1.72 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
# Time: O(n^4)
# Space: O(n)
import collections
# sort, brute force, hash table
class Solution(object):
def wordSquares(self, words):
"""
:type words: List[str]
:rtype: List[List[str]]
"""
words.sort()
lookup = collections.defaultdict(list)
for i, w in enumerate(words):
lookup[w[0]].append(i)
lookup[w[0], w[3]].append(i)
result = []
for i in xrange(len(words)):
for j in lookup[words[i][0]]:
if j == i:
continue
for k in lookup[words[i][3]]:
if k in (i, j):
continue
for l in lookup[words[j][3], words[k][3]]:
if l in (i, j, k):
continue
result.append([words[i], words[j], words[k], words[l]])
return result
# Time: O(n^4)
# Space: O(1)
# sort, brute force
class Solution2(object):
def wordSquares(self, words):
"""
:type words: List[str]
:rtype: List[List[str]]
"""
words.sort()
result = []
for i in xrange(len(words)):
for j in xrange(len(words)):
if j == i or words[j][0] != words[i][0]:
continue
for k in xrange(len(words)):
if k in (i, j) or words[k][0] != words[i][3]:
continue
for l in xrange(len(words)):
if l in (i, j, k) or words[l][0] != words[j][3] or words[l][3] != words[k][3]:
continue
result.append([words[i], words[j], words[k], words[l]])
return result