Word Ladder II

原创 2015年07月08日 20:54:10

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]

Return

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

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.


Solution:

class Solution {
public:
    vector<vector<string>> res;

    void dfsPath(unordered_map<string, unordered_set<string>> &path, vector<string> &temp, const string &key)
    {
        if(path[key].size() == 0)
        {
            temp.push_back(key);
            vector<string> tempPath = temp;
            reverse(tempPath.begin(), tempPath.end());
            res.push_back(tempPath);
            temp.pop_back();
            return ;
        }

        temp.push_back(key);
        for(unordered_set<string>::iterator iter = path[key].begin(); iter != path[key].end(); ++iter)
        {
            dfsPath(path, temp, *iter);
        }
        temp.pop_back();
    }

    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        res.clear();
        unordered_set<string> current;
        unordered_set<string> next;
        unordered_map<string, unordered_set<string>> path;

        if(dict.count(start) > 0) dict.erase(start);
        current.insert(start);

        while(current.count(end) == 0 && !dict.empty())
        {
            for(unordered_set<string>::iterator iter = current.begin(); iter != current.end(); ++iter)
            {
                string str = *iter;
                for(int i = 0; i < str.length(); ++i)
                {
                    for(char c = 'a'; c <= 'z'; ++c)
                    {
                        string tmp = str;
                        if(tmp[i] == c) continue;
                        tmp[i] = c;
                        if(dict.count(tmp) > 0)
                        {
                            path[tmp].insert(str);
                            next.insert(tmp);
                        }
                    }
                }
            }

            if(next.empty()) break;
            for(unordered_set<string>::iterator iter = next.begin(); iter != next.end(); ++iter)
            {
                dict.erase(*iter);
            }

            current = next;
            next.clear();
        }

        vector<string> temp;
        if(current.count(end) > 0) dfsPath(path, temp, end);

        return res;
    }
};


[LeetCode]Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from...

Leetcode: Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from...
  • doc_sgl
  • doc_sgl
  • 2013年10月28日 20:54
  • 7557

LeetCode :Word Ladder II My Solution

Word Ladder II  Total Accepted: 11755 Total Submissions: 102776My Submissions Given two ...
  • AresGod
  • AresGod
  • 2014年10月03日 23:25
  • 1306

leetcode-Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from...

Word Ladder II

Word Ladder II  Total Accepted: 1864 Total Submissions: 22064My Submissions   Given t...

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

leetcode: Word Ladder II

leetcode: Word Ladder II
  • hkx1030
  • hkx1030
  • 2014年06月25日 22:00
  • 583

[LeetCode] 126. Word Ladder II java

采用BFS广度搜索 先bfs找到endword,同时存储每个单词所处层数 再以endword为目标,往回找到startword,生成路径。 HashMap path = new HashMap()...

[leetcode]Word Ladder II

问题描述: Given two words (start and end), and a dictionary, find all shortest transformation sequenc...

LeetCode之Word Ladder II

/*BFS算法。此题与Word Ladder类似,但是需要记录路径。需要解决额外两个问题: 1.不能在遍历到当前单词时,删掉遍历到的单词,因为上一层中还未遍历的单词可能能到这个单词,所以只能记下来, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Word Ladder II
举报原因:
原因补充:

(最多只允许输入30个字)