leetcode: Word Ladder

本文详细阐述了优化后的BFS算法用于解决从起始单词到目标单词的最短转换路径问题,通过避免重复访问单词集,显著提高了算法效率。文中包括算法原理、代码实现以及实例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

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.

下面的算法超时

class Solution {
public:

    // BFS
    
    bool tryGetNextStrings(string curString, string destString, unordered_set<string> &dict, list<string> &nextList, set<string> &visitedDic)
    {
        int len = curString.size();
        
        for (int i=0; i<len; i++)
        {
            char curChar = curString.at(i); //get char one by one
            for (char c='a'; c !='z'; c++)
            {
                string tmpString = curString; //try new char in that position
                if (c != curChar)
                {
                    tmpString.at(i) = c;
                    
                    if (tmpString == destString)
                    {
                        return true;
                    }
                    
                    if (visitedDic.find(tmpString) != visitedDic.end())
                    {
                        continue;
                    }
                    
                    if (dict.find(tmpString) != dict.end())
                    {
                        nextList.push_back(tmpString);
                        visitedDic.insert(tmpString);
                    }
                }
            }
        }
        
        return false;
    }
    
    
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        
        if (start == end)
            return 0;
        
        list<string> preList;
        set<string> visitedDic;
        int count = 0;
        
        preList.push_back(start);
        visitedDic.insert(start);
        
        while (preList.size() > 0)
        {
            list<string> nextList;
            while (preList.size() > 0)
            {
                count++;
                string curString = preList.front();
                if (tryGetNextStrings(curString, end, dict, nextList, visitedDic))
                {
                    return count; 
                }
                preList.erase(preList.begin());
            }
            preList = nextList;
        }
        
        return 0;
    }
};




改进算法:  同时请参考 http://blog.youkuaiyun.com/zxzxy1988/article/details/8591890

class Solution {
public:

    // BFS
    
    bool tryGetNextStrings(string curString, string destString, unordered_set<string> &dict, list<string> &nextList)
    {
        int len = curString.size();
        
        for (int i=0; i<len; i++)
        {
            char curChar = curString.at(i); //get char one by one
            for (char c='a'; c !='z'; c++)
            {
                string tmpString = curString; //try new char in that position
                if (c != curChar)
                {
                    tmpString.at(i) = c;
                    
                    if (tmpString == destString)
                    {
                        return true;
                    }
                    
                    if (dict.find(tmpString) != dict.end())
                    {
                        nextList.push_back(tmpString);
                        dict.erase(tmpString); // very strange, we need to delete the the element in dic
                    }
                }
            }
        }
        
        return false;
    }
    
    
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        
        if (start == end)
            return 0;
        
        list<string> preList;
        int count = 1;
        
        preList.push_back(start);
        
        while (dict.size() > 0 && preList.size() > 0)
        {
            list<string> nextList;
            count++;
            while (preList.size() > 0)
            {
                string curString = preList.front();
                if (tryGetNextStrings(curString, end, dict, nextList))
                {
                    return count; 
                }
                preList.erase(preList.begin());
            }
            preList = nextList;
        }
        
        return 0;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值