leetcode 126. Word Ladder II

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
Only one letter can be changed at a time
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
Return
  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]
Note:
Return an empty list if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.


题目比较难,硬来会TLE。

为了节约时间,解法分为两步:

1、做一次BFS,给list里面每一个单词都做一次标记,标记和end之间的距离。

2、这样在之后的DFS的时候就能只向着距离变短的方向找。


class Solution {
public:
    vector<vector<string>> findLadders(string start, string end, vector<string>& wList) 
    {
        
        unordered_set<string> dict;   //unordered_set不需要额外去重
        for(int i = 0; i < wList.size(); i++)
            dict.insert(wList[i]);
        
        vector<vector<string>> ret;
        if (dict.find(end) == dict.end())
            return ret;
        else
            dict.erase(end);
        
        unordered_set<string> dic = dict;
        dic.insert(start);
        
        //BFS
        map<string, int> mmap;  //存每个string到end的距离
        queue<string> que;      //bfs标配,从end开始扩散
        que.push(end);
        int far = 0;            //far存到end的距离
        while(!que.empty())
        {
            int size = que.size();    
            for(int i = 0; i < size; i++)
            {
                string temp = que.front();    
                que.pop();
                mmap[temp] = far;
                for(int j = 0; j < temp.size(); j++)
                {
                    char oldchar = temp[j];
                    for(char a = 'a'; a <= 'z'; a++)
                    {
                        if(a == oldchar)
                            continue;
                        temp[j] = a;    
                        if (dic.find(temp) != dic.end())
                        {
                            que.push(temp);
                            dic.erase(temp);
                        }
                    }
                    temp[j] = oldchar;
                }
            }
            far++;
        }
        for(auto it = mmap.begin(); it != mmap.end() ;it++)
            cout<<it->first<<" "<<it->second<<endl;
        
        //DFS 只能用递归的这种方式
        vector<string> m1(1, start); //m1里面存的是过程中的解
        helper(ret, m1, dict, end, mmap);
        return ret;    
    }
    
    void helper(vector<vector<string>> & ret, vector<string> m1, unordered_set<string> &dict, string end, map<string, int> &mmap)
    {
        string start = m1[m1.size() - 1];
        int old_distance = mmap[start];
        for(int i = 0; i < start.size(); i++)    
        {
            char oldchar = start[i];
            for(char a = 'a'; a <= 'z'; a++)
            {
                start[i] = a;  
                if(start == end)
                {
                    m1.push_back(end);
                    ret.push_back(m1);
                    return;
                }
                
                if(dict.find(start) != dict.end() && mmap[start] < old_distance)
                {
                    m1.push_back(start);
                    helper(ret, m1, dict, end, mmap);
                    m1.pop_back();
                }
            }
            start[i] = oldchar;
        }
    }
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值