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:
- Only one letter can be changed at a time
- 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
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- [LeetCode] Word Ladder II
- Word Ladder II -- LeetCode
- Leetcode: Word Ladder II
- leetcode-Word Ladder II
- leetcode-Word Ladder II
- leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode: Word Ladder II
- Design Tic-Tac-Toe
- PKU四日游(信息科学夏令营)
- HDU 5761_2016 Multi-University Training Contest 3
- Leetcode Sum of Two Integers
- Leetcode Minimum Depth of Binary Tree
- Leetcode Word Ladder II
- Leetcode Binary Tree Preorder Traversal
- Leetcode Convert Sorted Array to Binary Search Tree
- Leetcode Missing Number
- Leetcode Fraction to Recurring Decimal
- Leetcode Rotate List
- typhon例子:cs,bufDataset(类似delphi的clientDataset),josn解析器
- java反射覆写toString方法实现输出实体类属性值
- 动态加载碎片的简单用法