美文网首页
Word Ladder

Word Ladder

作者: 极速魔法 | 来源:发表于2017-07-27 18:16 被阅读10次

    //127

    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.

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <map>
    
    
    using namespace std;
    
    class Solution{
    private:
        bool isSmilar(string a,string b){
            //only one char is diff
    
            //count diffs
            int cnt=0;
            for(int i=0;i<a.size();i++){
                if(a[i]!=b[i]){
                    cnt++;
                }
    
                if(cnt>=2){
                    return false;
                }
            }
    
            if(cnt==1){
                return true;
            } else {
                return false;
            }
        }
    public:
        int ladderLength(string beginWord,string endWord,vector<string> &wordList){
            queue<string> q;
            //note word isVisited
            map<string,bool > visited;
            //note distance from beginWord
            map<string,int> map;
    
    
            map[beginWord]=-1;
            //endWord may not in wordList,default map[endWord]=0
            //not exist may return 0+1
            map[endWord]=-1;
            for(int i=0;i<wordList.size();i++){
                visited[wordList[i]]=false;
                map[wordList[i]]=-1;
            }
    
    
    
            q.push(beginWord);
            //default is 0,after ++ become map[beginWord]=1
            map[beginWord]=0;
            visited[beginWord]=true;
            while(!q.empty()){
    
                string val = q.front();
                q.pop();
                if(val==endWord){
                    break;
                }
    
                for(int i=0;i<wordList.size();i++){
                    if(isSmilar(val,wordList[i])&& !visited[wordList[i]]){
                        q.push(wordList[i]);
                        map[wordList[i]]=map[val]+1;
                        visited[wordList[i]]=true;
                    }
                }
    
    
            }
    
            if(map[endWord]==-1){
                return 0;
            } else{
                //the lenth including beginWord
                return map[endWord]+1;
            }
        }
    
    };
    
    int main(){
        string beginWord="hit";
        string endWord="cog";
        int k=6;
        string s[k]={"hot","dot","dog","lot","log","cog"};
        vector<string> wordList(s,s+k);
        cout<<Solution().ladderLength(beginWord,endWord,wordList)<<endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:Word Ladder

          本文链接:https://www.haomeiwen.com/subject/gbrokxtx.html