forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordLadder.cpp
66 lines (57 loc) · 1.75 KB
/
wordLadder.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Source : https://oj.leetcode.com/problems/word-ladder/
// Author : Hao Chen
// Date : 2014-10-12
/**********************************************************************************
*
* Given two words (start and end), and a dictionary, find the length of shortest
* transformation sequence from start to end, such that:
*
* Only one letter can be changed at a time
* Each intermediate word must exist in the dictionary
*
* For example,
*
* Given:
* start = "hit"
* end = "cog"
* dict = ["hot","dot","dog","lot","log"]
*
* As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
* return its length 5.
*
* Note:
*
* Return 0 if there is no such transformation sequence.
* All words have the same length.
* All words contain only lowercase alphabetic characters.
*
*
**********************************************************************************/
class Solution {
public:
//BFS non-recursive method
int ladderLength(string start, string end, unordered_set<string> &dict) {
map<string, int> dis;
dis[start] = 1;
queue<string> q;
q.push(start);
while(!q.empty()){
string word = q.front();
q.pop();
if (word == end) {
break;
}
for (int i=0; i<word.size(); i++){
string temp = word;
for(char c='a'; c<='z'; c++){
temp[i] = c;
if (dict.count(temp)>0 && dis.count(temp)==0){
dis[temp] = dis[word] + 1;
q.push(temp);
}
}
}
}
return (dis.count(end)==0) ? 0 : dis[end];
}
};