leetcode 127. 单词接龙

题目:127. 单词接龙 - 力扣(LeetCode)

先建立一颗trie树,从beginWord开始bfs;bfs的过程中,对trie树进行dfs寻找"只差一个字母"的其他未遍历到的字符串;直到bfs遍历到endWord。

cpp 复制代码
struct Node {
    Node** c;
    string str;
    bool v = false;
    Node() {
        c = (Node**) malloc(26 * sizeof(Node*));
        memset(c, 0, 26 * sizeof(Node*));
    }
};
class Solution {
public:
    void dfs(list<string>& bfs, list<int>& val, const string& s, int path, Node* node, int idx, bool changed) {
        char c = s[idx] - 'a';
        Node* t;
        if (idx == s.length() - 1) {
            if (changed) {
                t = node->c[c];
                if (t && !t->v) {
                    t->v = true;
                    bfs.push_back(t->str);
                    val.push_back(path + 1);
                }
            } else {
                for (int i = 0; i < 26; i++) {
                    if (i == c) continue;
                    t = node->c[i];
                    if (t && !t->v) {
                        t->v = true;
                        bfs.push_back(t->str);
                        val.push_back(path + 1);
                    }
                }
            }
            return;
        }
        
        for (int i = 0; i < 26; i++) {
            if (!node->c[i]) {
                continue;
            }
            if (i == c) {
                dfs(bfs, val, s, path, node->c[i], idx + 1, changed);
            } else if (!changed) {
                dfs(bfs, val, s, path, node->c[i], idx + 1, true);
            }
        }
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        Node* root = new Node;
        bool hasEndWord = false;
        char c;
        Node* t;
        string s;
        for (int i = 0; i < wordList.size(); i++) {
            s = wordList[i];
            if (s.compare(endWord) == 0) {
                hasEndWord = true;
            }
            t = root;
            for (int j = 0; j < s.length(); j++) {
                c = s[j] - 'a';
                if (!t->c[c]) {
                    t->c[c] = new Node;
                }
                t = t->c[c];
            }
            t->str = s;
        }
        if (!hasEndWord) {
            return 0;
        }
        
        list<string> bfs;
        bfs.push_back(beginWord);
        list<int> val;
        val.push_back(1);
        int path;
        while (!bfs.empty()) {
            s = bfs.front();
            bfs.pop_front();
            path = val.front();
            val.pop_front();
//            printf("** %s %d\n", s.data(), path);
            if (s.compare(endWord) == 0) {
                return path;
            }
            dfs(bfs, val, s, path, root, 0, false);
        }
        
        return 0;
    }
};
相关推荐
老四啊laosi1 小时前
[双指针] 4. 力扣--盛最多水的容器
算法·leetcode·装水最多的容器
XiYang-DING1 小时前
【LeetCode】206. 反转链表
算法·leetcode·链表
_深海凉_2 小时前
LeetCode热题100-合并两个有序链表
算法·leetcode·链表
会编程的土豆2 小时前
【leetcode hot 100】依旧二叉树
算法·leetcode·职场和发展
浅念-2 小时前
LeetCode 双指针题型 C++ 解题整理
开发语言·数据结构·c++·笔记·算法·leetcode·职场和发展
Mr_Xuhhh2 小时前
LeetCode hot 100(C++版本)
c++·leetcode·哈希算法
Q741_1473 小时前
每日一题 力扣 3474. 字典序最小的生成字符串 贪心 字符串 C++ 题解
c++·算法·leetcode·贪心
人道领域3 小时前
LeetCode【刷题日记】:螺旋矩阵逆向全过程,边界缩进优化
算法·leetcode·矩阵
im_AMBER3 小时前
Leetcode 150 最小路径和 | 最长回文子串
数据结构·算法·leetcode
.柒宇.4 小时前
力扣hot 100之和为 K 的子数组(Java版)
java·算法·leetcode