您的位置:首页 > 其它

[LeetCode] Word Ladder

2015-06-25 01:00 375 查看
Well, this problem has a nice BFS structure.

Let's see the example in the problem statement.

start = "hit"


end = "cog"


dict = ["hot", "dot", "dog", "lot", "log"]


Since only one letter can be changed at a time, if we start from
"hit"
, we can only change to those words which have only one different letter from it, like
"hot"
. Putting in graph-theoretic terms, we can say that
"hot"
is a neighbor of
"hit"
.

The idea is simpy to begin from
start
, then visit its neighbors, then the non-visited neighbors of its neighbors... Well, this is just the typical BFS structure.

To simplify the problem, we insert
end
into
dict
. Once we meet
end
during the BFS, we know we have found the answer. We maintain a variable ladder for the current distance of the transformation and update it by
ladder++
once we finish a round of BFS search (note that it should fit the definition of the distance in the problem statement). Also, to avoid visiting a word for more than once, we erase it from
dict
once it is visited.

The code is as follows.

class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
wordDict.erase(beginWord);
wordDict.insert(endWord);
int ladder = 2, len = beginWord.length();
queue<string> nextWords;
nextWords.push(beginWord);
while (!nextWords.empty()) {
int num = nextWords.size();
for (int i = 0; i < num; i++) {
string word = nextWords.front();
nextWords.pop();
for (int p = 0; p < len; p++) {
char letter = word[p];
for (int j = 0; j < 26; j++) {
word[p] = 'a' + j;
if (wordDict.find(word) != wordDict.end()) {
if (word == endWord) return ladder;
wordDict.erase(word);
nextWords.push(word);
}
}
word[p] = letter;
}
}
ladder++;
}
return 0;
}
};


The above code can still be speed up if we also begin from
end
. Once we meet the same word from
start
and
end
, we know we are done. This link provides a nice two-end search solution. I rewrite the code below for better readability. At each round of BFS, depending on the relative size of nextWords and prevWords, we swap the smaller one to the working setto reduce the running time (lines 12 - 13).

class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
wordDict.erase(beginWord);
wordDict.erase(endWord);
unordered_set<string> nextWords;
unordered_set<string> prevWords;
nextWords.insert(beginWord);
prevWords.insert(endWord);
int ladder = 2, len = beginWord.length();
while (!nextWords.empty() && !prevWords.empty()) {
if (nextWords.size() > prevWords.size())
swap(nextWords, prevWords);
unordered_set<string>::iterator itr = nextWords.begin();
unordered_set<string> temp;
for (; itr != nextWords.end(); itr++) {
string word = *itr;
for (int p = 0; p < len; p++) {
char letter = word[p];
for (int j = 0; j < 26; j++) {
word[p] = 'a' + j;
if (prevWords.find(word) != prevWords.end())
return ladder;
if (wordDict.find(word) != wordDict.end()) {
temp.insert(word);
wordDict.erase(word);
}
}
word[p] = letter;
}
}
swap(nextWords, temp);
ladder++;
}
return 0;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: