Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27aed52

Browse files
committed
Sync LeetCode submission Runtime - 161 ms (71.55%), Memory - 36 MB (57.76%)
1 parent 866f5a4 commit 27aed52

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

‎0425-word-squares/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given an array of <strong>unique</strong> strings <code>words</code>, return <em>all the </em><strong><a href="https://en.wikipedia.org/wiki/Word_square" target="_blank">word squares</a></strong><em> you can build from </em><code>words</code>. The same word from <code>words</code> can be used <strong>multiple times</strong>. You can return the answer in <strong>any order</strong>.</p>
2+
3+
<p>A sequence of strings forms a valid <strong>word square</strong> if the <code>k<sup>th</sup></code> row and column read the same string, where <code>0 &lt;= k &lt; max(numRows, numColumns)</code>.</p>
4+
5+
<ul>
6+
<li>For example, the word sequence <code>[&quot;ball&quot;,&quot;area&quot;,&quot;lead&quot;,&quot;lady&quot;]</code> forms a word square because each word reads the same both horizontally and vertically.</li>
7+
</ul>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> words = [&quot;area&quot;,&quot;lead&quot;,&quot;wall&quot;,&quot;lady&quot;,&quot;ball&quot;]
14+
<strong>Output:</strong> [[&quot;ball&quot;,&quot;area&quot;,&quot;lead&quot;,&quot;lady&quot;],[&quot;wall&quot;,&quot;area&quot;,&quot;lead&quot;,&quot;lady&quot;]]
15+
<strong>Explanation:</strong>
16+
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> words = [&quot;abat&quot;,&quot;baba&quot;,&quot;atan&quot;,&quot;atal&quot;]
23+
<strong>Output:</strong> [[&quot;baba&quot;,&quot;abat&quot;,&quot;baba&quot;,&quot;atal&quot;],[&quot;baba&quot;,&quot;abat&quot;,&quot;baba&quot;,&quot;atan&quot;]]
24+
<strong>Explanation:</strong>
25+
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
33+
<li><code>1 &lt;= words[i].length &lt;= 4</code></li>
34+
<li>All <code>words[i]</code> have the same length.</li>
35+
<li><code>words[i]</code> consists of only lowercase English letters.</li>
36+
<li>All <code>words[i]</code> are <strong>unique</strong>.</li>
37+
</ul>

‎0425-word-squares/solution.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
3+
def wordSquares(self, words: List[str]) -> List[List[str]]:
4+
5+
self.words = words
6+
self.N = len(words[0])
7+
self.buildPrefixHashTable(self.words)
8+
9+
results = []
10+
word_squares = []
11+
for word in words:
12+
word_squares = [word]
13+
self.backtracking(1, word_squares, results)
14+
return results
15+
16+
def backtracking(self, step, word_squares, results):
17+
if step == self.N:
18+
results.append(word_squares[:])
19+
return
20+
21+
prefix = ''.join([word[step] for word in word_squares])
22+
for candidate in self.getWordsWithPrefix(prefix):
23+
word_squares.append(candidate)
24+
self.backtracking(step+1, word_squares, results)
25+
word_squares.pop()
26+
27+
def buildPrefixHashTable(self, words):
28+
self.prefixHashTable = {}
29+
for word in words:
30+
for prefix in (word[:i] for i in range(1, len(word))):
31+
self.prefixHashTable.setdefault(prefix, set()).add(word)
32+
33+
def getWordsWithPrefix(self, prefix):
34+
if prefix in self.prefixHashTable:
35+
return self.prefixHashTable[prefix]
36+
else:
37+
return set([])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /