leetcode127.单词接龙

题目大意

给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:

  • 每次转换只能改变一个字母。
  • 转换过程中的中间单词必须是字典中的单词。

说明:

  • 如果不存在这样的转换序列,返回 0。
  • 所有单词具有相同的长度。
  • 所有单词只由小写字母组成。
  • 字典中不存在重复的单词。
  • 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

示例 1:

输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

输出: 5

解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
     返回它的长度 5

示例 2:

输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

输出: 0

解释: endWord "cog" 不在字典中,所以无法进行转换。

解题思路

方法一(超时):

创建一个beginSet,初始化为beginword,遍历集合中的每个单词,遍历每个单词的每个位置,并依次用’a’~'z’进行替换(相当于找到修改一个字母后的单词的邻居)。如果替换后的单词等于endword,返回结果;如果替换后的单词在词表中且之前没有出现过,则加入beginset中。

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        if (find(wordList.begin(), wordList.end(), endWord) == wordList.end())
        	return 0;

        int charNum = beginWord.size();
        set<string> mySet;
        set<string> hasVisit;
        mySet.insert(beginWord);
        int res = 1;

        set<string> newSet;
        while (!mySet.empty()){
        	++res;
        	for (auto iter : mySet){
        		for (int i = 0; i < charNum; ++i){
        			char tmpChar = iter[i];
        			for (char item = 'a'; item <= 'z'; ++item){
        				iter[i] = item;
        				if (iter == endWord)
        					return res;
        				if (find(wordList.begin(), wordList.end(), iter) != wordList.end() && hasVisit.find(iter) == hasVisit.end()){
        					hasVisit.insert(iter);
        					newSet.insert(iter);
        				}
        			}
        			iter[i] = tmpChar;
        		}
        	}
        	swap(mySet, newSet);
        	newSet.clear();
        }
        return 0;
    }
};
方法二:

上面的方法中,不断寻找beginset中的单词的邻居,这使得beginset可能变得越来越大,因此耗时较多。
方法二中,可以采用双向搜索。再创建一个endset用来记录endword改变一个字符后的邻居。

注意:从beginword向endword搜索与从endword向beginword搜索效果相同。

我们每次选择beginset和endset中较小的set执行方法一中的操作即可。

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        if (find(wordList.begin(), wordList.end(), endWord) == wordList.end())
        	return 0;

        unordered_set<string> wordSet{wordList.begin(), wordList.end()};
        unordered_set<string> beginSet{beginWord};
        unordered_set<string> endSet{endWord};

        wordSet.erase(beginWord);

        int res = 1;

        while (!beginSet.empty()){
        	++res;
        	unordered_set<string> tmpSet;

        	for (auto word : beginSet){
        		for (int i = 0; i < word.size(); ++i){
        			char tmp = word[i];
        			for(char tmpChar = 'a'; tmpChar <= 'z'; ++tmpChar){
        				word[i] = tmpChar;
        				if (endSet.find(word) != endSet.end())
        					return res;
        				if (wordSet.find(word) != wordSet.end()){
        					wordSet.erase(word);
        					tmpSet.insert(word);
        				}
        			}
        			word[i] = tmp;
        		}
        	}
        	beginSet = tmpSet;
        	if(beginSet.size() > endSet.size())
        		swap(beginSet, endSet);
        }
        return 0;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_41092190/article/details/106466451