`
frank-liu
  • 浏览: 1666120 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: 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:

  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"]

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.

原问题链接:https://leetcode.com/problems/word-ladder/

 

问题分析

  如果不是充分利用问题中提到的所有字符都假定为小写的话,问题会显得更加复杂,解决思路也更加不好找。在给定的字符串里,我们如果要转换成下一个字符串,可能它里面的每个字符都变化了一个,那么对于它转换的下一个目标字符,就有它字符个数n x 26那么多种可能。而具体哪一种能最快或者说最近的转换成目标字符呢,这确实不好说。

  这里我们可以利用一个树的广度优先遍历的思路,首先从beginWord的第一个字符开始,设置它的值从'a'到'z',然后来判断它是否在给定的集合wordList里。当然,因为我们这么转换可能会变成前面已经转换过的元素了,所以这种情况要避免。为了避免这种情况,需要定义一个Map<String, Integer>,这里的key表示已经遍历过的元素,而且这些元素也存在于wordList中。value则表示是第几次转换。

  另外,既然是要按照广度遍历的方式,我们需要将每次生成的符合条件的字符串加入到一个队列中,以方便后面的进一步转换。

  所以,详细的代码实现如下: 

 

public class Solution {
    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        Map<String, Integer> map = new HashMap<>();
        map.put(beginWord, 1);
        Queue<String> queue = new LinkedList<>();
        queue.add(beginWord);
        while(!queue.isEmpty()) {
            String word = queue.remove();
            if(word.equals(endWord)) break;
            for(int i = 0; i < word.length(); i++) {
                char[] array = word.toCharArray();
                for(char c = 'a'; c <= 'z'; c++) {
                    array[i] = c;
                    String tmp = new String(array);
                    if(wordList.contains(tmp) && !map.containsKey(tmp)) {
                        map.put(tmp, map.get(word) + 1);
                        queue.add(tmp);
                    }
                }
            }
        }
        if(!map.containsKey(endWord)) return 0;
        return map.get(endWord);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics