[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:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

 

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

难度挺高的一题。沿用Word Ladder I的思路,此题本质也是图的遍历问题,求的是从给定起点到给定终点之间的所有最短路径。

Word Ladder I 中,找到一条合法路径即返回。然而此题中找到一条之后还需要进行讨论,问题就变复杂了,因为广度优先遍历中之前的节点都已经出队列没法再获取到。

所以,沿用Word Ladder I中的思想,不仅记录每个结点距离start的距离,而且用List记录这个到达这个节点的所有最短路径上的前驱结点。举个例子:

 

 

6的List应当为 2 和 3, 因为5 不是最短路径上的点。

这里采用HashMap去处理结点和List的映射关系。

这样在进行广度优先遍历过程中,会有以下几种情况:

1. 相邻结点新结点,那么新结点入队列;建立新结点的List和,把当前结点加入到新结点的List中;记录新结点的最短路径长度为当前结点的最短路径长度L+1

2. 相邻结点在队列中(例如上图结点3出队列时,6还在队列中)且相邻结点的最短路径长度=当前结点最短路径长度+1,那么说明是另一条最短路径,只需要把当前结点加入到相邻结点的List中。

3. 相邻结点在队列中且相邻结点的最短路径长度<当前结点最短路径长度+1,说明这条不是最短路径,那么什么也不做,当前结点默默出队列。

这样我们可以得到从start到end的若干条链,从end开始逆向使用回溯法记录所有的链上的结点即可。

代码如下:

 1     HashMap<String, ArrayList<String>> nodeSet = new HashMap<String, ArrayList<String>>();
 2     public List<List<String>> findLadders(String start, String end, Set<String> dict) {
 3         List<List<String>> re = new ArrayList<List<String>>();
 4         Queue<String> q = new LinkedList<String>();
 5         HashSet<String> hs = new HashSet<String>();
 6         HashMap<String, Integer> dist = new HashMap<String, Integer>();
 7         q.add(start);
 8         nodeSet.put(start, new ArrayList<String>());
 9         nodeSet.put(end, new ArrayList<String>());
10         dist.put(start, 1);
11         
12         while(!q.isEmpty()) {
13             String temp = q.poll();
14             int l = dist.get(temp);
15             hs.add(temp);
16             for(int i=0;i<temp.length();i++) {
17                 for(char c='a';c<='z';c++) {
18                     if(temp.charAt(i)==c)
19                         continue;
20                     StringBuilder sb = new StringBuilder(temp);
21                     sb.setCharAt(i,c);
22                     String next = sb.toString();
23                     if(next.equals(end)) {
24                         if(!dist.containsKey(end)) {
25                             dist.put(end,l+1);
26                             nodeSet.get(end).add(temp);
27                         }
28                         else if(dist.get(end)==l+1)
29                             nodeSet.get(end).add(temp);
30                     }
31                     else if(dict.contains(next) && !hs.contains(next)) {
32                         if(!dist.containsKey(next)) {
33                             q.add(next);
34                             dist.put(next, l+1);
35                             ArrayList<String> arr = new ArrayList<String>();
36                             arr.add(temp);
37                             nodeSet.put(next, arr);
38                         } else if(dist.get(next)==l+1)
39                             nodeSet.get(next).add(temp);
40                     }
41                 }
42             }
43         }
44         List<String> path = new ArrayList<String>();
45         path.add(end);
46         collect(start,re,path,nodeSet.get(end));
47         return re;
48     }
49     public void collect(String start, List<List<String>> re, List<String> path, ArrayList<String> prevNodes)
50     {
51         for(int i=0;i<prevNodes.size();i++)
52         {
53             path.add(0,prevNodes.get(i));
54             if(prevNodes.get(i).equals(start)) {
55                 List<String> pathCopy = new ArrayList<String>(path);
56                 re.add(pathCopy);
57             }
58             else
59                 collect(start,re,path,nodeSet.get(prevNodes.get(i)));
60             path.remove(0);
61         }
62     }

 

 

转载于:https://www.cnblogs.com/splash/p/4102786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Leetcode是一个在线的算法题库,Java是一种流行的编程语言。Leetcode上的题目可以用Java语言来解决。Leetcode上有大量的题目,覆盖了各种难度级别和算法类型,包括数组、字符串、链表、树、图、动态规划、排序、贪心、回溯等等。Java作为一种高级编程语言,可以快速并且简洁地解决Leetcode上的问题。此外,Java还有丰富的类库和框架,方便开发人员使用。如果你想使用Java来解决Leetcode的问题,可以在Leetcode上完成编码,然后在测试通过后,下载题目页面中的PDF文件,进行备份和归档,以便在以后需要的时候查看。 总而言之,Leetcode Java PDF可以让您用Java语言解决Leetcode上的问题,并将完成的代码用PDF文件格式化,以备将来参考。 ### 回答2: LeetCode是一个在线的编程题库,提供了2000多道算法题目,并且还提供了讨论和答案解释。其中,JavaLeetCode支持的编程语言之一,因此学习Java语言可以更好地完成LeetCode算法的练习。此外,LeetCode还提供了PDF文档,以便有需要的用户离线学习或分享给其他人。这些PDF文档包括Java编程语言的基础知识和LeetCode的算法解析。如果想要成为一个出色的Java工程师或算法专家,深入学习LeetCode并掌握Java编程语言是非常重要的。因此,练习LeetCode算法和阅读编程语言的PDF文档对于提升编程技能和职业发展是非常有帮助的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值