码迷,mamicode.com
首页 > 其他好文 > 详细

126. Word Ladder II

时间:2019-02-05 00:33:37      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:from   ems   tran   case   path   target   class   size   max   

Given two words (beginWord and endWord), and a dictionary‘s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return an empty list 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.

Example 1:

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

Output:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]

Example 2:

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

Output: []

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

 

Approach #1: DFS. [C++]

class Solution {
public:
    vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> visited;
        unordered_set<string> wordList_(wordList.begin(), wordList.end());
        vector<vector<string>> ans;
        queue<vector<string>> paths;
        paths.push({beginWord});
        // record length of current shortest transformation sequence
        int level = 1;
        // record lenght of last conditional shortest transformation sequence
        // if the target sequence‘s length shorter than minLevel we break the loop.
        // int minLevel = INT_MAX;
        
        while (!paths.empty()) {
            vector<string> path = paths.front();
            paths.pop();
            if (path.size() > level) {
                for (string s : visited) wordList_.erase(s);
                visited.clear();
                // if (path.size() > minLevel) {
                //     break;
                // } 
                level = path.size();
            }
            
            string last = path.back();
            for (int i = 0; i < last.length(); ++i) {
                string last_ = last;
                for (char c = ‘a‘; c <= ‘z‘; ++c) {
                    last_[i] = c;
                    if (wordList_.count(last_)) {
                        vector<string> newpath = path;
                        newpath.push_back(last_);
                        visited.insert(last_);
                        if (last_ == endWord) {
                            minLevel = level;
                            ans.push_back(newpath);
                        } else {
                            paths.push(newpath);
                        }
                    }
                }
            }
            
        }
        
        return ans;
    }
};

  

Analysis:

https://leetcode.com/problems/word-ladder-ii/discuss/40434/C%2B%2B-solution-using-standard-BFS-method-no-DFS-or-backtracking

 

126. Word Ladder II

标签:from   ems   tran   case   path   target   class   size   max   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10352397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!