Skip to content

127. Word Ladder

难度:Medium

刷题内容

原题连接

  • https://leetcode.com/problems/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.
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.
Example 1:

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

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:

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

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

˼·1

**- ʱ�临�Ӷ�: O(EV+V^3)*- �ռ临�Ӷ�: O(n^2)***

����һ内容描述�⣬��һ����BFS�⣬�������ǹ���һ��ͼ���ö�ά���鴢�棬Ȼ��BFSһ��һ内容描述����E�DZߵ�������v�ǽڵ������

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        wordList.push_back(beginWord);
        int len = wordList[0].length(),length = wordList.size();
        queue<int> q;
        vector<int> d[length];
        int min_l[length];
        for(int i = 0;i < length;++i)
            min_l[i] = INT_MAX;
        for(int i = 0;i < length;++i)
            for(int j = i + 1;j <length;++j)
            {
                int count1 = 0;
                for(int t = 0;t < len;++t)
                    if(wordList[i][t] != wordList[j][t])
                        count1++;
                if(count1 == 1)
                {
                    d[i].push_back(j);
                    d[j].push_back(i);
                }
            }
        int exit = -1;
        for(int i = 0;i < length;++i)
            if(wordList[i] == endWord)
            {
                exit = i;
                min_l[i] = 0;
                q.push(i);
            }
        if(exit == -1)
            return 0;
        while(q.size())
        {
            int t = q.front();
            for(int i = 0;i < d[t].size();++i)
                if(min_l[d[t][i]] > min_l[t] + 1)
                {
                    q.push(d[t][i]);
                    min_l[d[t][i]] = min_l[t] + 1;
                    if(d[t][i] == length - 1)
                        return min_l[length - 1] + 1;
                }
            q.pop();
        }
        return 0;
    }
};

˼·2

*- ʱ�临�Ӷ�: O(EVlen)*- �ռ临�Ӷ�: O(V)***

����ķ���Ч�ʲ��ߣ���˿��Ը�����unordered_set�������е��ַ�����Ȼ��ÿ��ֻ�Ķ�һ���ַ�����set�в内容描述���Ƿ���ڼ��ɡ������len��ÿ�����ʵij��ȡ�

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        wordList.push_back(beginWord);
        int len = wordList[0].length(),length = wordList.size();
        queue<string> q;
        unordered_set<string> s(wordList.begin(),wordList.end());
        int min_l[length];
        auto pos = s.find(endWord);
        if(pos == s.end())
            return 0;
        q.push(*pos);
        int level = 1;
        string count1 = endWord;
        while(q.size())
        {
            string temp1 = q.front();
            for(int j = 0;j < temp1.size();++j)
            {
                string temp = temp1;           
                for(int i = 'a';i <= 'z';++i)
                {
                    temp[j] = i;
                    if(temp != temp1 && s.find(temp) != s.end())
                    {
                        q.push(temp);
                        if(temp == beginWord)
                            return level + 1;
                        s.erase(temp);
                    }
                    temp = temp1;
                }
            }
            if(count1 == temp1)
            {
                count1 = q.back();
                level++;
            }
            s.erase(temp1);
            q.pop();
        }
        return 0;
    }
};

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组