前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode】Word Ladder

【leetcode】Word Ladder

作者头像
阳光岛主
发布2019-02-19 11:30:01
3830
发布2019-02-19 11:30:01
举报
文章被收录于专栏:米扑专栏米扑专栏

Question :

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.

For Example:

Graph Array =  [

h i t

h o t

d o t            

d o g           

l o t           

l o g

c o g

]

Anwser 1 :

代码语言:javascript
复制
class Solution {
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(start == end) return 0;      // shortest length
        
        queue <pair<string, int>> Q;
        map<string, bool> hmap;
        vector<string> vec;
        
        Q.push(make_pair(start, 1));        // start word
        
        while(!Q.empty()){
            pair<string, int> node = Q.front();
            string word = node.first;
            int level = node.second;
            
            if(word == end){
                break;
            }
            Q.pop();
            vec.clear();
            getWords(vec, word, hmap, dict);
            
            int len = vec.size();
            for(int i = 0; i < len; ++i){
                Q.push(make_pair(vec[i], level + 1));
            }
        }
        
        if(Q.empty()) {
            return 0;
        } else {
            return Q.front().second;
        }
    }
    
    void getWords(vector<string> &vec, string start, map<string, bool> &hmap, const unordered_set<string> &dict){
        int n = start.size();
        for(int i = 0; i < n; ++i)
        {
            string temp(start);
            for(int j = 0; j < 26; ++j)
            {
                temp[i] = j + 'a';      // a - z
                if(temp != start && dict.find(temp) != dict.end() && hmap.find(temp) == hmap.end())
                {
                    hmap.insert(make_pair(temp, false));
                    vec.push_back(temp);
                }
            }
        }
    }
};

Anwser 2 :  

代码语言:javascript
复制
class Solution {
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {        
        unordered_set<string> Set;
        queue<string> Q;
        int curLevel = 1;
        int curLevelNum = 1;
        int nextLevelNum = 0;
        
        Set.insert(start);
        Q.push(start);
        
        while (!Q.empty())
        {
            string cur = Q.front();
            Q.pop();
            
            --curLevelNum;
            for (unordered_set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
            {
                string temp = *it;
                if (canChange(temp, cur) && Set.find(temp) == Set.end())
                {
                    Q.push(temp);
                    Set.insert(temp);
                    ++nextLevelNum;
                }
            }
            
            if (canChange(cur, end)) return curLevel + 1;
            
            if (curLevelNum == 0)
            {
                ++curLevel;
                curLevelNum = nextLevelNum;
                nextLevelNum = 0;                
            }
        }
        
        return 0;
    }
    
    bool canChange(string a, string b)
    {
        if (a.size() != b.size())
            return false;
        
        int count = 0;
        for (int i = 0; i < a.size(); ++i)
        {
            if (a[i] != b[i])            
            {
                if (count == 1) return false;   // more than 1 letters diff, return
                ++count;
            }
        }
        
        return count == 1;      // only one letter diff
    }
};

参考推荐:

unordered_set

set

queue

map

vector

pair

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年04月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档