LeetCode126:Word Ladder

mickole發表於2014-04-27

題目:

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

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

For example,

Given:
start = "hit"
end = "cog"
dict = ["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.
  • 解題思路:
  • 這題一開始沒啥思路,谷歌一下,才知要用到BFS,既然要用到BFS,那當然形成一個抽象圖。這裡我們將每一個字串當做圖中一節點,如果兩字串只需通過變化一個字元即可相等,我們認為這兩字串相連。
  • 遍歷圖中節點時,我們通常會利用一個visit還標識是否訪問過,這裡我們將處理過的節點直接從dict中刪除,以免重複處理。
  • 實現程式碼:
  • #include <iostream>
    #include <string>
    #include <queue>
    #include <unordered_set>
    using namespace std;
    
    /*
    Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
    
    Only one letter can be changed at a time
    Each intermediate word must exist in the dictionary
    For example,
    
    Given:
    start = "hit"
    end = "cog"
    dict = ["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.
    */
    class Solution {
    public:
        int ladderLength(string start, string end, unordered_set<string> &dict) {
            if(start.empty() || end.empty() || dict.empty())
                return 0;
            queue<string> squ[2];//這裡需要用到兩個佇列,因為是bfs,按層遍歷,所以需要一層一層進行處理 
            squ[0].push(start);
            bool qid = false;
            int minLen = 1;
            while(!squ[qid].empty())
            {
                while(!squ[qid].empty())//處理同一層節點 
                {
                    string curstr = squ[qid].front();
                    squ[qid].pop();
                    for(int i = 0; i < curstr.size(); i++)
                    {
                        
                        for(char j = 'a'; j <= 'z'; j++)
                        {
                            if(j == curstr[i])
                                continue;
                            char t = curstr[i];
                            curstr[i] = j;
                            if(curstr == end)
                            {
                                return minLen+1;
                            }
                                
                            if(dict.count(curstr) > 0)
                            {
                                squ[!qid].push(curstr);
                                dict.erase(curstr);
                            }
                            curstr[i] = t;
                        }
                        
                    }
                                
                }
                qid = !qid;//表示將要處理的下一層 
                minLen++;
    
            }
            return 0;
            
        }
    };
    
    int main(void)
    {
        string start("hit");
        string end("cog");
        unordered_set<string> dict;
        dict.insert("hot");
        dict.insert("dot");
        dict.insert("dog");
        dict.insert("lot");
        dict.insert("log");
        Solution solution;
        int min = solution.ladderLength(start, end, dict);
        cout<<min<<endl;
        return 0;
    }

相關文章