leetcode 126. Word Ladder II

 1 import string
 2 import collections
 3
 4 class Solution(object):
 5     def findLadders(self, begin, end, words_list):
 6         ‘‘‘删除起至单词‘‘‘
 7         words_list.discard(begin)
 8         words_list.discard(end)
 9
10         ‘‘‘根据tree递归构造路径‘‘‘
11         def construct_paths(source, dest, tree):
12             if source == dest:
13                 return [[source]]
14             return [[source] + path for succ in tree[source]
15                                     for path in construct_paths(succ, dest, tree)]
16
17         ‘‘‘广度优先遍历分层搜索‘‘‘
18         def bfs_level(this_lev, endw, tree, words_set):
19             if not this_lev: return False
20             for word in (this_lev):
21                 words_set.discard(word)
22             next_lev, done = set(), False
23             while this_lev:
24                 word = this_lev.pop()
25                 for c in string.ascii_lowercase:
26                     for index in range(len(word)):
27                         neigh = word[:index] + c + word[index+1:]
28                         if neigh == endw:
29                             done = True
30                             tree[word].append( neigh )
31                         if not done and neigh in words_set:
32                             next_lev.add(neigh)
33                             tree[word].append( neigh )
34             return done or bfs_level(next_lev, endw, tree, words_set)
35
36
37         tree, path, paths = collections.defaultdict(list), [begin], []
38         is_found = bfs_level(set([begin]), end, tree, words_list)
39         return construct_paths(begin, end, tree)
时间: 2024-10-14 12:24:51

leetcode 126. Word Ladder II的相关文章

Java for LeetCode 126 Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time    Each intermediate word must exist in the dictionary For example, Given:start = "

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

[LeetCode] 126. Word Ladder II_Hard tag: BFS&DFS

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in the word list. Note

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit

【leetcode】126. Word Ladder II

题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从startword开始到wordlist每一个word的最小转换次数,这一点非常重要,可以过滤很多无效的运算. 代码如下: class Solution(object): def getLadderList(self, w,d): l = [] r = [] for i in xrange(26): l.a

126. Word Ladder II

class StringWithLevel { String str; int level; public StringWithLevel(String str, int level) { this.str = str; this.level = level; } } public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) { Arra

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

[python leetcode] Word Ladder II (very hard)[非常难,放弃]

Word Ladder II 描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: • Only one letter can be changed at a time • Each intermediate word must exist in the dictionary For examp