127. Word Ladder

欢迎fork and star:Nowcoder-Repository-github

127. Word Ladder

题目

 Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence 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"]

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.
    You may assume no duplicates in the word list.
    You may assume beginWord and endWord are non-empty and are not the same.

UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. 

解析

  • bfs,严格按照每一层计数++
class Solution_127_old {

        //主要思想:广度优先搜索。先构造一个字符串队列,并将start加入队列。1.对队列头字符串做单个字符替换
        //每次替换后,2.判断是否和end匹配,如果匹配,返回答案;3.没有匹配,则在字典里面查询是否有“邻居字符串”,
        //如果有,则将该字符串加入队列,同时将该字符串从字典里删除。重复1的过程,知道和end匹配。如果最后队列
        //为空还未匹配到,则返回0.
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        if (start==end||dict.empty())
        {
            return 0;
        }
        int ret = 1;

        queue<string> que; //bfs
        que.push(start);
        while (!que.empty())
        {
            int size = que.size();  //层次遍历,记录当前队列大小

            while (size--)  //不加这层循环,ret++是对每次元素出队列就计数;加循环就是严格每层计数++
            {
                string temp = que.front();
                que.pop();
                for (int i = 0; i < temp.size(); i++)
                {
                    char ch = temp[i]; //对每个单词的每次字符替换构成新的单词,查看是否在字典中

                    for (int j = 0; j < 26; j++) //对同一个单词处理,ret++不应该放在里面
                    {
                        temp[i] = 'a' + j; //替换
                        if (dict.find(temp) != dict.end()) //找到了  需要吗?&&temp[i]!=ch不影响答案
                        {
                            //ret++; //找到一个计数+1 
                            if (temp == end)
                            {
                                return ret + 1;  //找到答案,退出
                            }
                            que.push(temp);
                            dict.erase(temp);
                        }
                    }
                    temp[i] = ch; //还原单词
                }
            }
            ret++; 
        }

        return 0;
    }
};

class Solution_127 {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        string start = beginWord;
        string end = endWord;
        unordered_set<string> dict(wordList.begin(), wordList.end());

        if (start == end || dict.empty())
        {
            return 0;
        }
        int ret = 1;

        queue<string> que; //bfs
        que.push(start);
        while (!que.empty())
        {
            int size = que.size();  //层次遍历,记录当前队列大小

            while (size--)  //不加这层循环,ret++是对每次元素出队列就计数;加循环就是严格每层计数++
            {
                string temp = que.front();
                que.pop();
                for (int i = 0; i < temp.size(); i++)
                {
                    char ch = temp[i]; //对每个单词的每次字符替换构成新的单词,查看是否在字典中

                    for (int j = 0; j < 26; j++) //对同一个单词处理,ret++不应该放在里面
                    {
                        temp[i] = 'a' + j; //替换
                        if (dict.find(temp) != dict.end()) //找到了  需要吗?&&temp[i]!=ch不影响答案
                        {
                            //ret++; //找到一个计数+1 
                            if (temp == end)
                            {
                                return ret + 1;  //找到答案,退出
                            }
                            que.push(temp);
                            dict.erase(temp);
                        }
                    }
                    temp[i] = ch; //还原单词
                }
            }
            ret++;
        }

        return 0;
    }
};

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8178949.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞