首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 127 Word Ladder

Leetcode 127 Word Ladder

作者头像
triplebee
发布2018-01-12 14:51:41
4840
发布2018-01-12 14:51:41
举报

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord to endWord, such that:

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

For example,

Given: beginWord = "hit" endWord = "cog" wordList = ["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.

每个单词每次只能变动一个字母变到字典中的其他单词,问从beginword变到endword最少需要多少步?

BFS,维护一个vis,表示已经到达过的单词,这样下次可以遇到可以不用搜索。

第一次提交超时了,以为BFS写搓了,调了半天发现是判断相邻单词的步骤超时。

原来版本的相邻词判断是这样的

    bool judge(string a,string b)
    {
        bool flag=false;
        for(int i=0;i<a.size();i++)
        {
            if(a[i]!=b[i])
            {
                if(flag) return false;
                flag=true;
            }
        }
        return flag;
    }
        for(unordered_set<string>::iterator it=wordList.begin();it!=wordList.end();it++)
        {
            unordered_set<string>::iterator it2=it;
            for(it2++;it2!=wordList.end();it2++)
                if(judge(*it,*it2))
                {
                    mp[*it].push_back(*it2);
                    mp[*it2].push_back(*it);
                }
        }

怎么样?遍历所有单词组合判断他们是否相邻,复杂度n*n*len(word),

改成下面的做法过了,

对于每一个词,枚举每一位的变化为其他字母的情况,判断这个情况是否在集合中,复杂度n*len(word)*26*1,确实要快上一些,学到了!

class Solution {
public:
    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        unordered_map<string,vector<string>> mp;
        wordList.insert(beginWord);
        wordList.insert(endWord);
        for(unordered_set<string>::iterator it=wordList.begin();it!=wordList.end();it++)
        {
            string cc=*it;
            for(int i=0;i<cc.size();i++)
            {
                string c=*it;
                for(char j='a';j<='z';j++)
                {
                    c[i]=j;
                    if(wordList.find(c)!=wordList.end())
                    {
                        mp[*it].push_back(c);
                    }
                }
            }
        }
        queue<string> q,q2;
        unordered_map<string,bool> vis;
        q.push(beginWord);
        vis[beginWord]=true;
        int res=0,flag=1;
        while(!q.empty() || !q2.empty())
        {
            if(!q.empty())
            {
                string temp=q.front();
                q.pop();
                if(temp==endWord)
                {
                    res=flag;
                    break;
                }
                for(int i=0;i<mp[temp].size();i++)
                {
                    string tt=mp[temp][i];
                    if(vis[tt]) continue;
                    vis[tt]=true;
                    q2.push(tt);
                }
            }
            else
            {
                flag++;
                swap(q,q2);
            }
        }
        
        return res;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档