Leetcode Word Ladder II

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord toendWord, such that:

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

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

Difficulty: Hard


Solution: BFS

public class Solution {
    public String replace(String s, int index, char c) {
        char[] chars = s.toCharArray();
        chars[index] = c;
        return new String(chars);
    }
    
    public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) {
        int curr_step = Integer.MAX_VALUE;
        List<List<String>> result = new ArrayList<List<String>>();
        if(beginWord.equals(endWord)){
            List<String> tempList = new ArrayList<String>();
            tempList.add(beginWord);
            result.add(tempList);
            return result;
        }
        if(wordList.size() == 0) return result;
        Queue<wordNode> q = new LinkedList<wordNode>();
        HashSet<String> visited = new HashSet<String>();
        HashSet<String> unvisited = new HashSet<String>();
        int depth = 0;
        wordList.add(endWord);
        q.add(new wordNode(beginWord, 0, null));
        visited.add(beginWord);
        unvisited.addAll(wordList);
        unvisited.remove(beginWord);
        while(!q.isEmpty()){
            wordNode node = q.poll();
            if(node.step >= curr_step){
                break;
            }
            else{
                if(node.step > depth){
                    unvisited.removeAll(visited);
                }
                depth = node.step;
                for(int i = 0; i < node.word.length(); i++){
                    for(char c = 'a'; c <= 'z'; c++){
                        String newWord = replace(node.word, i, c);
                        if(newWord.equals(endWord)){
                            curr_step = node.step+1;
                            List<String> list = new ArrayList<String>();
                            list.add(endWord);
                            wordNode tempNode = node;
                            while(tempNode != null){
                                list.add(0, tempNode.word);
                                tempNode = tempNode.pre;
                            }
                            result.add(list);
                        }
                        else if(unvisited.contains(newWord)){
                            visited.add(newWord);
                            q.add(new wordNode(newWord, node.step + 1, node));
                        }
                        else{
                            continue;
                        }
                    }
                }
            }
            
        }
        return result;
        
    }
}


class wordNode{
    String word;
    int step;
    wordNode pre;
    
    public wordNode(String word, int step, wordNode pre){
        this.word = word;
        this.step = step;
        this.pre = pre;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值