-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladderlength.h
63 lines (57 loc) · 1.68 KB
/
ladderlength.h
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
//
// Created by so_go on 2019/6/17.
//
#ifndef SRC_LADDERLENGTH_H
#define SRC_LADDERLENGTH_H
#include<string>
#include<vector>
#include<climits>
#include"printMatrix.h"
#include<queue>
#include<unordered_set>
using namespace std;
class LadderLength {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int num_nodes = wordList.size() + 1;
unordered_set<string> wordSet(wordList.begin(), wordList.end());
if(wordSet.find(endWord) == wordSet.end()){
cout << "End word not found." << endl;
return 0;
}
return bfs(beginWord, endWord, wordSet);
}
struct Record{
string str;
int depth;
};
int bfs(string initial, string target, unordered_set<string> wordSet){
queue<Record> q;
// initial
q.push(Record{initial, 1});
string alphebet = "abcdefghijklmnopqrstuvwxyz";
Record cur;
string newStr;
while(not q.empty()){
cur = q.front();
q.pop();
cout << cur.str << ' ' << cur.depth << endl;
if(cur.str == target){
return cur.depth;
}
for(int i = 0; i < cur.str.size(); i++){
for(int j = 0; j < 26; j++) {
newStr = cur.str;
newStr[i] = alphebet[j];
auto newStr_ptr = wordSet.find(newStr);
if(newStr_ptr != wordSet.end()){
q.push({newStr, cur.depth + 1});
wordSet.erase(newStr_ptr);
}
}
}
}
return 0;
}
};
#endif //SRC_LADDERLENGTH_H