LeetCode 127. Word Ladder
原题链接在这里:https://leetcode.com/problems/word-ladder/
题目:
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
题解:
用BFS找最短路径。先把beginWord放入queue中,level 设值为1. 在queue不为空的情况下,从queue中拿出一个word, 设为cur, 同时curCount--.
从cur的第一个字母开始,从a 到 z 挨个替换,若是等于了endWord, 直接返回level+1即可。否则就看set 中是否有这个变形,若是有就把他添加到queue中,并增加计数nextCount. 为了保证不陷入infinite loop, 需同时从set中remove掉这个词。
每当curCont == 0时,说明本层已经扫描完,level++, 同时更新curCount 为nextCount, nextCount 为 0.
Time Complexity: O(m*n). m = wordList.size(). n = beginWord.length().
Space: O(m*n). queue把所有的word都加了进去.
AC Java:
1 class Solution { 2 public int ladderLength(String beginWord, String endWord, List<String> wordList) { 3 if(wordList == null || wordList.size() == 0){ 4 return 0; 5 } 6 7 HashSet<String> dic = new HashSet<>(); 8 dic.addAll(wordList); 9 10 if(!dic.contains(endWord)){ 11 return 0; 12 } 13 14 int level = 1; 15 LinkedList<String> que = new LinkedList<>(); 16 que.add(beginWord); 17 while(!que.isEmpty()){ 18 int size = que.size(); 19 20 while(size-- > 0){ 21 String cur = que.poll(); 22 if(cur.equals(endWord)){ 23 return level; 24 } 25 26 for(int i = 0; i < cur.length(); i++){ 27 char [] arr = cur.toCharArray(); 28 char ori = arr[i]; 29 for(char k = 'a'; k <= 'z'; k++){ 30 if(ori == k){ 31 continue; 32 } 33 34 arr[i] = k; 35 String can = new String(arr); 36 if(dic.contains(can)){ 37 dic.remove(can); 38 que.add(can); 39 } 40 } 41 } 42 } 43 44 level++; 45 } 46 47 return 0; 48 } 49 }
Could optimize with bidirectional BFS.
Time Complexity: O(m * n). m = wordList.size(). n = word.length().
Space: O(m * n).
AC Java:
1 class Solution { 2 public int ladderLength(String beginWord, String endWord, List<String> wordList) { 3 if(wordList == null || wordList.size() == 0){ 4 return 0; 5 } 6 7 HashSet<String> dic = new HashSet<>(); 8 dic.addAll(wordList); 9 10 if(!dic.contains(endWord)){ 11 return 0; 12 } 13 14 HashSet<String> startSet = new HashSet<String>(); 15 startSet.add(beginWord); 16 HashSet<String> endSet = new HashSet<String>(); 17 endSet.add(endWord); 18 HashSet<String> visited = new HashSet<String>(); 19 visited.add(beginWord); 20 21 int level = 1; 22 while(!startSet.isEmpty()){ 23 if(startSet.size() > endSet.size()){ 24 HashSet<String> temp = startSet; 25 startSet = endSet; 26 endSet = temp; 27 } 28 29 HashSet<String> nextSet = new HashSet<String>(); 30 for(String cur : startSet){ 31 for(int i = 0; i < cur.length(); i++){ 32 char [] arr = cur.toCharArray(); 33 char ori = arr[i]; 34 for(char k = 'a'; k <= 'z'; k++){ 35 if(k == ori){ 36 continue; 37 } 38 39 arr[i] = k; 40 String can = new String(arr); 41 if(endSet.contains(can)){ 42 return level + 1; 43 } 44 45 if(dic.contains(can) && visited.add(can)){ 46 nextSet.add(can); 47 } 48 } 49 } 50 } 51 52 startSet = nextSet; 53 level++; 54 } 55 56 return 0; 57 } 58 }