Skip to content

Commit f21cc92

Browse files
committed
Word Ladder/ Word Break II
1 parent a7f4305 commit f21cc92

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

127 Word Ladder.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
3+
4+
Only one letter can be changed at a time
5+
Each intermediate word must exist in the word list
6+
For example,
7+
8+
Given:
9+
beginWord = "hit"
10+
endWord = "cog"
11+
wordList = ["hot","dot","dog","lot","log"]
12+
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
13+
return its length 5.
14+
15+
Note:
16+
Return 0 if there is no such transformation sequence.
17+
All words have the same length.
18+
All words contain only lowercase alphabetic characters.
19+
'''
20+
21+
class Solution(object):
22+
def ladderLength(self, beginWord, endWord, wordList):
23+
"""
24+
:type beginWord: str
25+
:type endWord: str
26+
:type wordList: Set[str]
27+
:rtype: int
28+
"""
29+
wordList.add(endWord)
30+
cur_level = [beginWord]
31+
next_level = []
32+
depth = 1
33+
n = len(beginWord)
34+
while cur_level:
35+
for item in cur_level:
36+
if item == endWord:
37+
return depth
38+
for i in range(n):
39+
for c in 'abcdefghijklmnopqrstuvwxyz':
40+
word = item[:i] + c + item[i + 1:]
41+
if word in wordList:
42+
wordList.remove(word)
43+
next_level.append(word)
44+
depth += 1
45+
cur_level = next_level
46+
next_level = []
47+
return 0
48+
49+
50+
if __name__ == "__main__":
51+
assert Solution().ladderLength("hit", "cog", {"hot", "dot", "dog", "lot", "log"}) == 5

140 Word Break II.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
3+
4+
Return all such possible sentences.
5+
6+
For example, given
7+
s = "catsanddog",
8+
dict = ["cat", "cats", "and", "sand", "dog"].
9+
10+
A solution is ["cats and dog", "cat sand dog"].
11+
'''
12+
13+
import collections
14+
15+
16+
class Solution(object):
17+
def wordBreak(self, s, wordDict):
18+
"""
19+
:type s: str
20+
:type wordDict: Set[str]
21+
:rtype: List[str]
22+
"""
23+
dic = collections.defaultdict(list)
24+
25+
def dfs(s):
26+
if not s:
27+
return [None]
28+
if s in dic:
29+
return dic[s]
30+
res = []
31+
for word in wordDict:
32+
n = len(word)
33+
if s[:n] == word:
34+
for r in dfs(s[n:]):
35+
if r:
36+
res.append(word + " " + r)
37+
else:
38+
res.append(word)
39+
dic[s] = res
40+
return res
41+
42+
return dfs(s)
43+
44+
45+
if __name__ == "__main__":
46+
assert Solution().wordBreak("catsanddog", {"cat", "cats", "and", "sand", "dog"}) == ['cat sand dog', 'cats and dog']

0 commit comments

Comments
 (0)