LeetCode //C - 127. Word Ladder

127. Word Ladder

A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:

  • Every adjacent pair of words differs by a single letter.
  • Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
  • sk == endWord

Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.

Example 1:

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.

Example 2:

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.

Constraints:
  • 1 <= beginWord.length <= 10
  • endWord.length == beginWord.length
  • 1 <= wordList.length <= 5000
  • wordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i] consist of lowercase English letters.
  • beginWord != endWord
  • All the words in wordList are unique.

From: LeetCode

Link: 127. Word Ladder


Solution:

Ideas:
  1. Start with beginWord "hit" and level 1.
  • Queue = ["hit"]
  1. Dequeue "hit" and explore its neighbors:
  • Check each word in wordList to find neighbors of "hit" (words that are one letter different and unvisited).
  • "hot" is a neighbor. Mark "hot" as visited and enqueue it.
  • Queue = ["hot"]
  • Increase level to 2.
  1. Dequeue "hot" and explore its neighbors:
  • Find neighbors of "hot" (unvisited words in wordList that are one letter different).
  • "dot" and "lot" are neighbors. Mark them as visited and enqueue them.
  • Queue = ["dot", "lot"]
  • Increase level to 3.
  1. Dequeue "dot" and explore its neighbors:
  • Find neighbors of "dot".
  • "dog" is a neighbor. Mark "dog" as visited and enqueue it.
  • Queue = ["lot", "dog"]
  1. Dequeue "lot" and explore its neighbors:
  • Find neighbors of "lot".
  • "log" is a neighbor. Mark "log" as visited and enqueue it.
  • Queue = ["dog", "log"]
  • Increase level to 4.
  1. Dequeue "dog" and explore its neighbors:
  • Find neighbors of "dog".
  • "cog" is a neighbor. Mark "cog" as visited and enqueue it.
  • Queue = ["log", "cog"]
  1. Dequeue "log" and explore its neighbors:
  • Find neighbors of "log".
  • No unvisited neighbors.
  • Queue = ["cog"]
  1. Dequeue "cog" and explore its neighbors:
  • "cog" is the endWord.
  • Return level which is 5, representing the shortest transformation sequence: "hit" -> "hot" -> "dot" -> "dog" -> "cog".
Code:
c 复制代码
bool isOneLetterDiff(char *a, char *b) {
    int diffCount = 0;
    while (*a) {
        if (*(a++) != *(b++)) diffCount++;
        if (diffCount > 1) return false;
    }
    return diffCount == 1;
}

int ladderLength(char *beginWord, char *endWord, char **wordList, int wordListSize) {
    if (!beginWord || !endWord || !wordList || wordListSize == 0) return 0;
    
    bool *visited = (bool *)calloc(wordListSize, sizeof(bool));
    char **queue = (char **)malloc(sizeof(char *) * (wordListSize + 1));
    int front = 0, rear = 0;
    
    queue[rear++] = beginWord;
    int level = 1;
    
    while (front < rear) {
        int size = rear - front;
        for (int i = 0; i < size; i++) {
            char *word = queue[front++];
            if (strcmp(word, endWord) == 0) {
                free(visited);
                free(queue);
                return level;
            }
            for (int j = 0; j < wordListSize; j++) {
                if (!visited[j] && isOneLetterDiff(word, wordList[j])) {
                    visited[j] = true;
                    queue[rear++] = wordList[j];
                }
            }
        }
        level++;
    }
    
    free(visited);
    free(queue);
    return 0;
}
相关推荐
汀、人工智能34 分钟前
[特殊字符] 第2课:字母异位词分组
数据结构·算法·链表·数据库架构··字母异位词分组
良木生香1 小时前
【C++初阶】:C++类和对象(下):构造函数promax & 类型转换 & static & 友元 & 内部类 & 匿名对象 & 超级优化
c语言·开发语言·c++
小O的算法实验室1 小时前
2026年SEVC,面向主动成像卫星任务规划问题的群体智能与动态规划混合框架,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
网安INF1 小时前
数据结构第一章复习:基本概念与算法复杂度分析
数据结构·算法
无巧不成书02181 小时前
C语言零基础速通指南 | 1小时从入门到跑通完整项目
c语言·开发语言·编程实战·c语言入门·零基础编程·c语言速通
幻风_huanfeng2 小时前
人工智能之数学基础:什么是凸优化问题?
人工智能·算法·机器学习·凸优化
三雷科技2 小时前
使用 `dlopen` 动态加载 `.so` 文件
开发语言·c++·算法
Yzzz-F2 小时前
Problem - 2146D1 - Codeforces &&Problem - D2 - Codeforces
算法
Kk.08022 小时前
力扣 LCR 084.全排列||
算法·leetcode·职场和发展
环黄金线HHJX.2 小时前
龙虾钳足启发的AI集群语言交互新范式
开发语言·人工智能·算法·编辑器·交互