Word Ladder

//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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 昨天我和老妈出去买衣服,是独立品牌店的那种。那里一共分了三层,第二层是女装。于是我们上了楼,在楼梯拐角处摆了两套风...
    娱乐天空m阅读 448评论 1 0
  • 突然觉得应该教孩子处理问题,不是一味的告诉他应该做到什么结果。 还是我教育方式不对。
    不笑也倾城阅读 168评论 0 0
  • 晨起6点半,排骨花生粥用高压锅煮上,然后去美宜佳买了4支牙刷,去菜摊买了三种青菜,回来路上遇到鑫慧妈妈,看她提着很...
    向往精灵阅读 153评论 0 0
  • 07 最后的最后 在学校最后的日子里,见到那些新鲜的面孔,就觉得羡慕。他们那么年轻,有大把的时间可以浪费,而我们却...
    穆念晴阅读 323评论 7 2
  • 践行|Alben李辰光|007-679我们在保护着什么 金句: 在当今世界的你我,有那么多人以身示范地在自我成长的...
    no_repeat阅读 371评论 0 0