leetcode #127 in cpp

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord toendWord, 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.


Solution:

Since we are to get shortest length, we should perform BFS. In BFS, we scan through current words in queue. For each word, we change one letter and see if new word in in the dictionary. If so we put the new word in queue.  By this we reach the endWord with fewest levels. This gives us the shortest length of change from beginword to e

Code:

class Solution {
public:
    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        if(beginWord == endWord) {
            
            return 0;
        }
        
        deque<string> next;//BFS traversal 
        next.push_back(beginWord);
        if(wordList.find(beginWord)!=wordList.end()) wordList.erase(beginWord);
        
        int step = 0;
        
        bool isfound = false; 
        while(!next.empty()){
            int cap = next.size(); 
<span style="white-space:pre">	</span>//for current level, collect the next level
            while(cap>0){
                string word = next.front(); 
                next.pop_front();
                if(word == endWord){//if we meet endword, we terminate.
                    return step+1;
                }else{
<span style="white-space:pre">		</span>//change one letter only
                    for(int j = 0; j < word.length(); j ++){
                        char c = word[j];
                        for(int i = 'a'; i <='z'; i++){
                            if(c == i) continue;
                            word[j] = i;
<span style="white-space:pre">			</span>     //after changing one letter, if the new word in in the dictionary, push it into queue
                            if(wordList.find(word)!=wordList.end()){
                                next.push_back(word);
                            }
                        }
                        word[j] = c; 
                    }
                }
                cap--;
            }
<span style="white-space:pre">		</span>//erase the word we have met in the dictionary. This prevents going back and forth like hot -> dot->hot->dot.
            for(int i = 0; i < next.size(); i ++){
                wordList.erase(next[i]);
            }
            if(isfound) break;
            step ++; 
        }
        return 0;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值