面试算法108:单词演变

题目

输入两个长度相同但内容不同的单词(beginWord和endWord)和一个单词列表,求从beginWord到endWord的演变序列的最短长度,要求每步只能改变单词中的一个字母,并且演变过程中每步得到的单词都必须在给定的单词列表中。如果不能从beginWord演变到endWord,则返回0。假设所有单词只包含英文小写字母。

例如,如果beginWord为"hit",endWord为"cog",单词列表为["hot","dot","dog","lot","log","cog"],则演变序列的最短长度为5,一个可行的演变序列为"hit"→"hot"→"dot"→"dog"→"cog"。

分析

应用图相关算法的前提是找出图中的节点和边。这个问题是关于单词的演变的,所以每个单词就是图中的一个节点。如果两个单词能够相互演变(改变一个单词的一个字母能变成另一个单词),那么这两个单词之间有一条边相连。

为了求得两个节点之间的最短距离,常见的解法是用两个队列实现广度优先搜索算法。一个队列queue1中存放离起始节点距离为d的节点,当从这个队列中取出节点并访问的时候,与队列queue1中节点相邻的节点离起始节点的距离都是d+1,将这些相邻的节点存放到另一个队列queue2中。当队列queue1中的所有节点都访问完毕时,再访问队列queue2中的节点,并将相邻的节点放入queue1中。可以交替使用queue1和queue2这两个队列由近及远地从起始节点开始搜索所有节点。

解: 单向广度优先搜索

java 复制代码
public class Test {
    public static void main(String[] args) {
        List<String> wordList = Arrays.asList("hot", "dot", "dog", "lot", "log", "cog");
        int result = ladderLength("hit", "cog", wordList);
        System.out.println(result);
    }

    public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Queue<String> queue1 = new LinkedList<>();
        Queue<String> queue2 = new LinkedList<>();
        Set<String> notVisited = new HashSet<>(wordList);
        int length = 1;
        queue1.add(beginWord);
        while (!queue1.isEmpty()) {
            String word = queue1.remove();
            if (word.equals(endWord)) {
                return length;
            }

            List<String> neighbors = getNeighbors(word);
            for (String neighbor : neighbors) {
                if (notVisited.contains(neighbor)) {
                    queue2.add(neighbor);
                    notVisited.remove(neighbor);
                }
            }

            if (queue1.isEmpty()) {
                length++;
                queue1 = queue2;
                queue2 = new LinkedList<>();
            }
        }

        return 0;
    }

    private static List<String> getNeighbors(String word) {
        List<String> neighbors = new LinkedList<>();
        char[] chars = word.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char old = chars[i];

            for (char ch = 'a'; ch <= 'z'; ++ch) {
                if (old != ch) {
                    chars[i] = ch;
                    neighbors.add(new String(chars));
                }
            }

            chars[i] = old;
        }

        return neighbors;
    }

}
相关推荐
十八旬3 分钟前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
Hilaku9 分钟前
面试官开始问我AI了,前端的危机真的来了吗?
前端·javascript·面试
脑洞代码12 分钟前
20250909的学习笔记
算法
Christo312 分钟前
TFS-2003《A Contribution to Convergence Theory of Fuzzy c-Means and Derivatives》
人工智能·算法·机器学习
Java微观世界13 分钟前
匿名内部类和 Lambda 表达式为何要求外部变量是 final 或等效 final?原理与解决方案
java·后端
黑菜钟20 分钟前
代码随想录第七天|● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 18.四数之和
c++·算法·leetcode
SimonKing31 分钟前
全面解决中文乱码问题:从诊断到根治
java·后端·程序员
Yingjun Mo33 分钟前
1. 统计推断-ALMOND收敛性分析
人工智能·算法·机器学习
海梨花41 分钟前
CSP认证练习题目推荐 (1)
算法·深度优先·csp
你三大爷1 小时前
再探volatile原理
java