127. 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 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
构建set,首先将step全部为1的word加入set, 然后2, 3, ...直到有word为end
然后,为了节约时间,我们用two end, 这就意味着有一个begin set, 一个end set, 当begin set的size大于end set时,我们就从endSet开始找。
plus,为了避免重复的寻找,我们用一个visited set, 如果已经visited过, 就不考虑。

public class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> dict) {
        Set<String> wordList = new HashSet<>(dict);
        Set<String> beginSet = new HashSet<String>(), endSet = new HashSet<String>();

        int len = 1;
        int strLen = beginWord.length();
        HashSet<String> visited = new HashSet<String>();
        
        beginSet.add(beginWord);
        endSet.add(endWord);
        while (!beginSet.isEmpty() && !endSet.isEmpty()) {
            if (beginSet.size() > endSet.size()) {
                Set<String> set = beginSet;
                beginSet = endSet;
                endSet = set;
            }

            Set<String> temp = new HashSet<String>();
            for (String word : beginSet) {
                char[] chs = word.toCharArray();

                for (int i = 0; i < chs.length; i++) {
                    for (char c = 'a'; c <= 'z'; c++) {
                        char old = chs[i];
                        chs[i] = c;
                        String target = String.valueOf(chs);

                        if (endSet.contains(target)) {
                            return len + 1;
                        }

                        if (!visited.contains(target) && wordList.contains(target)) {
                            temp.add(target);
                            visited.add(target);
                        }
                        chs[i] = old;
                    }
                }
            }

            beginSet = temp;
            len++;
        }
        
        return 0;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Given two words (beginWord and endWord), and a dictionary...
    ShutLove阅读 500评论 0 0
  • 问题 Given two words (beginWord and endWord), and a diction...
    RobotBerry阅读 173评论 2 1
  • 2010-10 1.C++中注释“//”的有效范围:A开始到行尾 2.假定MYClass是一个类 则该类的复制构造...
    aofeilin阅读 450评论 1 2
  • CFAbsoluteTimeGetCurrent()函数能够以毫秒为单位计算当前时间与2001.1.1差值。
    August24阅读 7,456评论 0 2
  • 我问小伙伴 :“现在有滴滴打车,专车,为什么还要买车?” 他 : “那不一样,有自己的车肯定自在好多嘛,想去哪里就...
    任梦晨阅读 179评论 0 0