Skip to content

Commit 9fcc7fa

Browse files
committed
Sync LeetCode submission Runtime - 296 ms (49.26%), Memory - 18.7 MB (77.36%)
1 parent faa45c0 commit 9fcc7fa

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

0127-word-ladder/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>A <strong>transformation sequence</strong> from word <code>beginWord</code> to word <code>endWord</code> using a dictionary <code>wordList</code> is a sequence of words <code>beginWord -&gt; s<sub>1</sub> -&gt; s<sub>2</sub> -&gt; ... -&gt; s<sub>k</sub></code> such that:</p>
2+
3+
<ul>
4+
<li>Every adjacent pair of words differs by a single letter.</li>
5+
<li>Every <code>s<sub>i</sub></code> for <code>1 &lt;= i &lt;= k</code> is in <code>wordList</code>. Note that <code>beginWord</code> does not need to be in <code>wordList</code>.</li>
6+
<li><code>s<sub>k</sub> == endWord</code></li>
7+
</ul>
8+
9+
<p>Given two words, <code>beginWord</code> and <code>endWord</code>, and a dictionary <code>wordList</code>, return <em>the <strong>number of words</strong> in the <strong>shortest transformation sequence</strong> from</em> <code>beginWord</code> <em>to</em> <code>endWord</code><em>, or </em><code>0</code><em> if no such sequence exists.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> beginWord = &quot;hit&quot;, endWord = &quot;cog&quot;, wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]
16+
<strong>Output:</strong> 5
17+
<strong>Explanation:</strong> One shortest transformation sequence is &quot;hit&quot; -&gt; &quot;hot&quot; -&gt; &quot;dot&quot; -&gt; &quot;dog&quot; -&gt; cog&quot;, which is 5 words long.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> beginWord = &quot;hit&quot;, endWord = &quot;cog&quot;, wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;]
24+
<strong>Output:</strong> 0
25+
<strong>Explanation:</strong> The endWord &quot;cog&quot; is not in wordList, therefore there is no valid transformation sequence.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= beginWord.length &lt;= 10</code></li>
33+
<li><code>endWord.length == beginWord.length</code></li>
34+
<li><code>1 &lt;= wordList.length &lt;= 5000</code></li>
35+
<li><code>wordList[i].length == beginWord.length</code></li>
36+
<li><code>beginWord</code>, <code>endWord</code>, and <code>wordList[i]</code> consist of lowercase English letters.</li>
37+
<li><code>beginWord != endWord</code></li>
38+
<li>All the words in <code>wordList</code> are <strong>unique</strong>.</li>
39+
</ul>

0127-word-ladder/solution.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach: Breadth First Search
2+
3+
# n = len(wordList), l = length of each word
4+
# Time: O(n * 26 * l)
5+
# Space: O(n)
6+
7+
import string
8+
from collections import deque, defaultdict
9+
10+
class Solution:
11+
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
12+
if endWord not in wordList:
13+
return 0
14+
15+
wordList = set(wordList)
16+
17+
queue = deque([(beginWord, 1)])
18+
visited = {beginWord}
19+
20+
while queue:
21+
word, level = queue.popleft()
22+
23+
if word == endWord:
24+
return level
25+
26+
for i in range(len(word)):
27+
for c in string.ascii_lowercase:
28+
next_word = word[:i] + c + word[i + 1 :]
29+
30+
if next_word in wordList and next_word not in visited:
31+
visited.add(next_word)
32+
queue.append((next_word, level + 1))
33+
34+
return 0
35+

0 commit comments

Comments
 (0)