ORIGIN

LeetCode 127. Word Ladder

ACM 2 mins366 words

Problem Description

https://leetcode.com/problems/word-ladder/

Analysis

It’s kind of tricky for me to understand the transform sequence. This means s1->s2 there is only on character changed. eg. abc -> aec the middle character changed.

Then, the problem is to find the next word in the wordList.

A simple way to do that is we replace each position of the current word with characters a - z. So we check if this new word in wordList.

Then the rest would be BFS template.

In order to avoid loop in the word sequence, we need to remove the word we used from wordList.

The reason why its no affect to remove it right after it’s pushed to queue is the followings:

The possibility word sequences from this word will be the same no matter what the previous words are. For example, there are some possible sequences A -> B -> C->End, A -> B -> D->End. if there is other sequence that can also reaches B (F->G->B) the possible sequences will be: F->G->B->C->End, F->G->B->D->End. You can see that the longer sequences has no use to exists. So we don’t need to let the longer one appear, we can safely remove the word B just when we met it.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> words(wordList.begin(), wordList.end());
queue<string> que;
que.push(beginWord);
int len = 1;
while(!que.empty()) {
len ++;
int n = que.size();
for(int k = 0; k < n; k ++) {
string t = que.front();
que.pop();
for(int i = 0; i < t.length(); i ++) {
for(int j = 'a'; j <= 'z'; j ++) {
char c = t[i];
t[i] = j;
if(words.find(t) != words.end()) {
if(t == endWord) {
return len;
}
que.push(t);
words.erase(t); // this word is being used.
}
t[i] = c; // replace it back
}
}
}

}
return 0;
}
};
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|