127. 单词接龙

Problem: 127. 单词接龙

思路

给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0。很显然这道题目可以用双向广搜来解决。

解题方法

将开始字符添加到小的一侧(set),将结束字符添加到大的一侧(set)。
从小的一侧开始拓展,暴力展开,如果大的一侧中有与当前字符相同的就说明找到了,两侧连通起来的,返回当前的len即可,每变换一次在总的字典中寻找如果存在,删除记录防止重复也就是剪枝。交换的过程:如果当前层小于等于较大层,交换当前层和较小层,反之,当前层大于较大层,交换当前层和较大层,较大层和较小层

复杂度

时间复杂度:

时间复杂度方面,由于我们同时从两个方向进行搜索,所以时间复杂度会相对较低。具体的时间复杂度取决于搜索的层数,最坏情况下为 O ( M ∗ N 2 ) O(M*N^2) O(MN2),其中M为wordList的长度,N为单词的平均长度。

空间复杂度:

空间复杂度方面,我们使用了三个集合smallLevel、bigLevel和nextLevel来存储单词,以及一个HashSet来存储总词汇表。所以空间复杂度为 O ( M ) O(M) O(M),其中M为wordList的长度。

Code

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        // 总词汇表
        Set<String> dict = new HashSet<>(wordList);
        if (!dict.contains(endWord)) {
            return 0;
        }
        // 数据量小的一侧
        Set<String> smallLevel = new HashSet<>();
        // 数据量大的一侧
        Set<String> bigLevel = new HashSet<>();
        // 数据量小的下一侧
        Set<String> nextLevel = new HashSet<>();
        smallLevel.add(beginWord);
        bigLevel.add(endWord);
        for (int len = 2; !smallLevel.isEmpty(); len++) {
            for (String w : smallLevel) {
                // 从小的一侧拓展
                char[] word = w.toCharArray();
                for (int j = 0; j < word.length; j++) {
                    char old = word[j];
                    for (char change = 'a'; change <= 'z'; change++) {
                        if (change != old) {
                            word[j] = change;
                            String next = String.valueOf(word);
                            if (bigLevel.contains(next)) {
                                return len;
                            }
                            if (dict.contains(next)) {
                                dict.remove(next);
                                nextLevel.add(next);
                            }
                        }
                    }
                    word[j] = old;
                }

            }
            if (nextLevel.size() <= bigLevel.size()) {
                // 交换当前层和较小层
                Set<String> temp = smallLevel;
                smallLevel = nextLevel;
                nextLevel = temp;
            } else {
                // 当前层大于较大层 交换当前层和较大层 较大层和较小层
                Set<String> temp = smallLevel;
                smallLevel = bigLevel;
                bigLevel = nextLevel;
                nextLevel = temp;
            }
            nextLevel.clear();
        }
        return 0;
    }
}

相关推荐

  1. 127. 单词

    2024-02-07 15:46:01       36 阅读
  2. 127. 单词

    2024-02-07 15:46:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-07 15:46:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-07 15:46:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-07 15:46:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-07 15:46:01       20 阅读

热门阅读

  1. 【ESLint】TypeError:this.libOptions.parse is not a function

    2024-02-07 15:46:01       32 阅读
  2. 09-错误处理

    2024-02-07 15:46:01       27 阅读
  3. HarmonyOS NEXT 星河版项目案例

    2024-02-07 15:46:01       33 阅读
  4. MybatisPlus Wrapper构造器(查询篇)

    2024-02-07 15:46:01       26 阅读
  5. 【算法题】93. 复原 IP 地址

    2024-02-07 15:46:01       28 阅读
  6. R语言入门笔记2.3

    2024-02-07 15:46:01       28 阅读
  7. WebSocketServer依赖注入问题

    2024-02-07 15:46:01       30 阅读
  8. Android设置默认8时区和默认24小时制

    2024-02-07 15:46:01       34 阅读