-
Notifications
You must be signed in to change notification settings - Fork 481
/
0127.cpp
50 lines (49 loc) · 1.53 KB
/
0127.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
#include <string>
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
int step = 1;
unordered_set<string> wordDict(wordList.begin(), wordList.end());
if (wordDict.count(endWord) == 0) return 0;
unordered_set<string> s1{beginWord};
unordered_set<string> s2{endWord};
while (!s1.empty())
{
unordered_set<string> stack;
for (auto s : s1) wordDict.erase(s);
for (auto s : s1)
{
for (unsigned int i = 0; i < beginWord.size(); ++i)
{
for (char c = 'a'; c <= 'z'; ++c)
{
string tmp = s;
tmp[i] = c;
if (!wordDict.count(tmp)) continue;
if (s2.count(tmp)) return ++step;
stack.insert(tmp);
}
}
}
s1 = stack.size() < s2.size() ? stack : s2;
s2 = stack.size() < s2.size() ? s2 : stack;
++step;
}
return 0;
}
};
int main()
{
string beginWord = "hit";
string endWord = "cog";
vector<string> wordList = {"hot","dot","dog","lot","log","cog"};
cout << Solution().ladderLength(beginWord, endWord, wordList) << endl;
return 0;
}