【LeetCode】#126单词接龙II(Word Ladder II)

【LeetCode】#126单词接龙II(Word Ladder II)

题目描述

给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:
1.每次转换只能改变一个字母。
2.转换过程中的中间单词必须是字典中的单词。
说明:
1.如果不存在这样的转换序列,返回一个空列表。
2.所有单词具有相同的长度。
3.所有单词只由小写字母组成。
4.字典中不存在重复的单词。
5.你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

示例

示例 1:

输入:
beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]
输出:
[
[“hit”,“hot”,“dot”,“dog”,“cog”],
[“hit”,“hot”,“lot”,“log”,“cog”]
]

示例 2:

输入:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,“dot”,“dog”,“lot”,“log”]
输出: []
解释: endWord “cog” 不在字典中,所以不存在符合要求的转换序列。

Description

Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
1.Only one letter can be changed at a time
2.Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
1.Return an empty list if there is no such transformation sequence.
2.All words have the same length.
3.All words contain only lowercase alphabetic characters.
4.You may assume no duplicates in the word list.
5.You may assume beginWord and endWord are non-empty and are not the same.

Example

Example 1:

Input:
beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]
Output:
[
[“hit”,“hot”,“dot”,“dog”,“cog”],
[“hit”,“hot”,“lot”,“log”,“cog”]
]

Example 2:

Input:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,“dot”,“dog”,“lot”,“log”]
Output: []
Explanation: The endWord “cog” is not in wordList, therefore no possible transformation.

解法

public class Solution {
    private List<List<String>> listList = new ArrayList<>();
    private List<String> list = new ArrayList<>();
    private boolean[][] graph;
    private int[] d;
    private final int INF = 1000000000;
    private int[] countInq;
    private boolean[] inq;
    private int size;
    private ArrayList<HashSet<Integer>> pre;
    private List<Integer> tempPath = new ArrayList<>();
    private int start = 0, end = 0;
 
    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        HashSet<String> hashSet = new HashSet<>();
        hashSet.addAll(wordList);
        hashSet.add(beginWord);
        if(!hashSet.contains(endWord)){
            return listList;
        }
        int index = 0;
        for(String s : hashSet){
            list.add(s);
            if(s.equals(beginWord)){
                start = index;
            }
            if(s.equals(endWord)){
                end = index;
            }
            index++;
        }
        size = list.size();
        graph = new boolean[size][size];
        for(int i = 0; i < size; i++){
            for(int j = i + 1; j < size; j++){
                if(hashPath(list.get(i), list.get(j))){
                    graph[i][j] = graph[j][i] = true;
                }
            }
        }
        d = new int[size];
        Arrays.fill(d, INF);
        countInq = new int[size];
        Arrays.fill(countInq, 0);
        inq = new boolean[size];
        pre = new ArrayList<>();
        for(int i = 0; i < size; i++){
            pre.add(new HashSet<>());
        }
        spfa(start);
        dfs(end);
        return listList;
    }
 
    private boolean hashPath(String s1, String s2){
        int count = 0;
        for(int i = 0; i < s1.length(); i++){
            if(s1.charAt(i) != s2.charAt(i)){
                count++;
            }
        }
        if(1 == count){
            return true;
        }
        return false;
    }
 
    private boolean spfa(int s){
        d[s] = 0;
        Queue<Integer> queue = new LinkedList<>();
        queue.add(s);
        countInq[s]++;
        inq[s] = true;
        while(!queue.isEmpty()){
            int u = queue.poll();
            inq[u] = false;
            for(int v = 0; v < size; v++){
                if(graph[u][v]){
                    if(d[u] + 1 < d[v]) {
                        pre.get(v).clear();
                        pre.get(v).add(u);
                        d[v] = d[u] + 1;
                        if (!inq[v]) {
                            queue.add(v);
                            countInq[v]++;
                            inq[v] = true;
                            if (countInq[v] > size - 1) {
                                return false;
                            }
                        }
                    }else if(d[u] + 1 == d[v]){
                        pre.get(v).add(u);
                        if (!inq[v]) {
                            queue.add(v);
                            countInq[v]++;
                            inq[v] = true;
                            if (countInq[v] > size - 1) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
 
    private void dfs(int nowVisit){
        tempPath.add(nowVisit);
        if(nowVisit == start){
            List<String> path = new ArrayList<>();
            for(int i = tempPath.size() - 1; i >= 0; i--){
                path.add(list.get(tempPath.get(i)));
            }
            listList.add(path);
            tempPath.remove(tempPath.size() - 1);
            return;
        }
        for(Integer integer : pre.get(nowVisit)){
            dfs(integer);
        }
        tempPath.remove(tempPath.size() - 1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值