LeetCode之Word Ladder

来源:互联网 发布:网络流行语 英文 编辑:程序博客网 时间:2024/04/28 03:11

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.
Run Status: Accepted!
Program Runtime: 12 milli secs


class Solution {

    
    private:
        int difchar(string s, string e)
        {
            int len = s.length();
            int num = 0;
            
            for(int i = 0; i < len; i++)
            {
                if(s[i]!=e[i])
                    num++;        
            }    
            
            return num;
        }      
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<string> search;
       
        
         if(difchar(start, end) == 0)
            {
                
                return 1;                  
            }   
            if(difchar(start, end) == 1)
            {
            
                return 2;                  
            }   
        
        unordered_set<string>::iterator it = dict.begin();
        
        while(it!=dict.end())
        {
                
            if((*it) == start || (*it) == end)
               it = dict.erase(it);
            else
                it++;
        }
        
        if(dict.empty())
            return 0;
            
        it = dict.begin();
        
        while(it!=dict.end())
        {
           
            if(difchar(start, *it) == 1)
            {
                search.push_back(*it);
                it = dict.erase(it);     
            }      
            else 
                 it++;            
        }
        
        if(search.empty())
            return 0;
        
        vector<string>::iterator ip = search.begin();
         int min = 0;
        while(ip!=search.end())
        {
            int n = ladderLength(*ip, end, dict);          
            if(n == 0)
            {
              ip++;
              continue;
            }
            
             if(min == 0)
                 min = n+1;
            else if(min>n+1)
                min = n+1;
                
            ip++;
        }
        
        
        return min;  
        
    }

};


测试用例:

inputoutputexpected "a", "c", ["a","b","c"]22
 
"hot", "dog", ["hot","dog"]00
 
"hot", "dog", ["hot","dog","dot"]33
 
"hot", "dot", ["hot","dot","dog"]22
 
"hot", "dog", ["hot","cog","dog","tot","hog","hop","pot","dot"]33
 
"hot", "dog", ["hot","dog","cog","pot","dot"]33
 
"hit", "cog", ["hot","cog","dot","dog","hit","lot","log"]55
 
"hit", "cog", ["hot","hit","cog","dot","dog"]55
 
"red", "tax", ["ted","tex","red","tax","tad","den","rex","pee"]44
 
"lost", "cost", ["most","fist","lost","cost","fish"]22
 
"lost", "miss", ["most","mist","miss","lost","fist","fish"]44
 
"leet", "code", ["lest","leet","lose","code","lode","robe","lost"]66
 
"talk", "tail", ["talk","tons","fall","tail","gale","hall","negs"]00