Leetcode Word Ladder II

来源:互联网 发布:linux高级运维 编辑:程序博客网 时间:2024/06/08 07:35

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
原创粉丝点击