关闭

Leetcode163: Word Ladder

标签: algorithmleetcode
174人阅读 评论(0) 收藏 举报
分类:

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:

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

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",

return its length 5.

网上对该问题的解决无一例外都是按照下面的思路:将当前单词每一个字符替换成a~z的任意一个字符,然后判断是否在词典中出现。此时的复杂度是O(26*word_length),当单词比较短时,这种方法的优势就体现出来了。

class Solution {
public:
    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        if(beginWord.size() != endWord.size()) return 0;  
        if(beginWord.empty() || endWord.empty())return 0;  
          
        queue<string> path;  
        path.push(beginWord);  
        int level = 1;  
        int count = 1;  
        wordList.erase(beginWord);  
        while(wordList.size() > 0 && !path.empty())  
        {  
            string curword = path.front();  
            path.pop();count--;  
            for(int i = 0; i < curword.size(); i++)  
            {  
                string tmp = curword;  
                for(char j='a'; j<='z'; j++)  
                {  
                    if(tmp[i]==j)continue;  
                    tmp[i] = j;  
                    if(tmp==endWord)return level+1;  
                    if(wordList.find(tmp) != wordList.end()) path.push(tmp);  
                    wordList.erase(tmp);  
                }  
            }  
            if(count==0)  
            {  
                count = path.size();  
                level++;  
            }  
        }  
        return 0;  
    }
};


0
0
查看评论

Leetcode Word Ladder II 解题报告

http://oj.leetcode.com/problems/word-ladder-ii/ Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from s...
  • worldwindjp
  • worldwindjp
  • 2014-02-16 22:40
  • 9728

[Leetcode]127. Word Ladder@python

题目Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord...
  • qian2729
  • qian2729
  • 2016-01-24 22:33
  • 649

Word Ladder -- LeetCode

原题链接: http://oj.leetcode.com/problems/word-ladder/  这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方...
  • linhuanmars
  • linhuanmars
  • 2014-04-06 15:10
  • 19399

LeetCode 127 Word Ladder java (看某位大神的解法做出来的)

问题重述: Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to e...
  • qq_16166139
  • qq_16166139
  • 2016-04-21 21:50
  • 1003

LeetCode127—Word Ladder

LeetCode127—Word Ladder
  • zhangxiao93
  • zhangxiao93
  • 2016-05-09 17:37
  • 1717

Word Ladder II -- LeetCode

原题链接: http://oj.leetcode.com/problems/word-ladder-ii/  这道题是LeetCode中AC率最低的题目,确实是比较难。一方面是因为对时间有比较严格的要求(容易超时),另一方面是它有很多细节需要实现。思路上和Word Ladd...
  • linhuanmars
  • linhuanmars
  • 2014-04-07 04:34
  • 17713

LeetCode126—Word Ladder II

LeetCode126—Word Ladder II
  • zhangxiao93
  • zhangxiao93
  • 2016-05-26 08:41
  • 2198

[leetcode-127]Word Ladder(java)

问题如下: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such ...
  • zdavb
  • zdavb
  • 2015-08-31 16:25
  • 1611

Leetcode 127 Word Ladder I

这道题目推荐和Leetcode 102 Binary Tree Level Order Traversal(http://blog.csdn.net/zxzxy1988/article/details/8597354)连起来看 这道题目首先想到的是DFS,或曰backtracking,也就...
  • zxzxy1988
  • zxzxy1988
  • 2013-02-19 17:05
  • 16032

LeetCode[127. Word Ladder] 题解 难度[medium]

C++语言,使用BFS解决word ladder问题
  • cs_linyf
  • cs_linyf
  • 2016-09-06 15:14
  • 340
    个人资料
    • 访问:84399次
    • 积分:2494
    • 等级:
    • 排名:第17425名
    • 原创:148篇
    • 转载:119篇
    • 译文:0篇
    • 评论:6条
    个人主页
    文章分类
    最新评论