leetcode-WordLadder

发布时间:2019-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了leetcode-WordLadder脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

题目:
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 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.

由于只需要求最短的距离,所以直接BFS就行了。这题OJ的数据卡得比较严,裸BFS会TLE。没有必要写双向的BFS,注意下剪枝就可以了。直接放代码。
code:

import java.util.*;

public class WordLadder {
    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        LinkedList<String> queue = new LinkedList<String>();
        
        queue.offer(beginWord);
        map.put(beginWord, 1);
        boolean flag = false;
        while(!queue.isEmpty()){
            String curr = queue.poll();
            for(String next : getNextWords(curr, wordList)){
                if(next.equals(endWord)){
                    flag = true;
                    map.put(next, map.get(curr) + 1);
                    System.out.println(next + " " + map.get(next));
                    return map.get(next);
                }
                if(wordList.contains(next)){
                    if(!map.containsKey(next)){
                        map.put(next, map.get(curr) + 1);
                        System.out.println(next + " " + map.get(next));
                        queue.offer(next);
                    }
                }
            }
        }
        if(flag) return map.get(endWord);
        else return 0;
    }
    
    public List<String> getNextWords(String word, Set<String> wordList){
        List<String> nextWords = new ArrayList<String>();
        for(int i = 0; i < word.length(); i++){
            for(char c = 'a'; c <= 'z'; c++){
                if(c == word.charAt(i)) continue;
                char[] arr = word.toCharArray();
                arr[i] = c;
                String next = new String(arr);
                if(wordList.contains(next)) nextWords.add(next);
            }
        }
        return nextWords;
    }
    
    public static void main(String[] args){
        WordLadder WL = new WordLadder();
        Set<String> wordList = new HashSet<String>();
        wordList.add("a");
        wordList.add("b");
        wordList.add("c");
        String beginWord = "a";
        String endWord = "c";
        int result = WL.ladderLength(beginWord, endWord, wordList);
        System.out.println(result);
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的leetcode-WordLadder全部内容,希望文章能够帮你解决leetcode-WordLadder所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。