forked from MaskRay/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-ladder.cc
145 lines (140 loc) · 3.67 KB
/
word-ladder.cc
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Word Ladder
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
unordered_map<string, int> d;
queue<string> q;
d[start] = 0;
dict.insert(end);
for (q.push(start); ! q.empty(); ) {
string i = q.front();
int dd = d[i];
q.pop();
REP(j, i.size()) {
char cc = i[j];
FOR(c, 'a', 'z'+1) {
i[j] = c;
if (dict.count(i)) {
dict.erase(i);
q.push(i);
d[i] = dd+1;
}
}
i[j] = cc;
}
}
return d.count(end) ? d[end]+1 : 0;
}
};
/// hamming distance trick
class Solution {
bool hamming_one(const string &a, const string &b) {
int i = 0, j = a.size();
while (i < j && a[i] == b[i]) i++;
while (i < j && a[j-1] == b[j-1]) j--;
return i == j-1;
}
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
unordered_map<string, int> d;
unordered_map<string, vector<string>> left, right;
queue<string> q;
d[start] = 0;
dict.erase(start);
dict.insert(end);
int n = start.length();
for (auto &x: dict) {
string l = x.substr(0, n/2), r = x.substr(n/2);
left[l].push_back(r);
right[r].push_back(l);
}
for (q.push(start); ! q.empty(); ) {
string x = q.front(), l = x.substr(0, n/2), r = x.substr(n/2);
int dd = d[x];
q.pop();
if (left.count(l))
for (auto &y: left[l])
if (hamming_one(r, y)) {
string z = l+y;
if (dict.count(z)) {
dict.erase(z);
q.push(z);
d[z] = dd+1;
}
}
if (right.count(r))
for (auto &y: right[r])
if (hamming_one(l, y)) {
string z = y+r;
if (dict.count(z)) {
dict.erase(z);
q.push(z);
d[z] = dd+1;
}
}
}
return d.count(end) ? d[end]+1 : 0;
}
};
/// bidirectional BFS + hamming distance trick
class Solution {
bool hamming_one(const string &a, const string &b) {
int i = 0, j = a.size();
while (i < j && a[i] == b[i]) i++;
while (i < j && a[j-1] == b[j-1]) j--;
return i == j-1;
}
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
unordered_map<string, vector<string>> left, right;
int n = start.length(), d = 1;
dict.insert(start);
dict.insert(end);
for (auto &x: dict) {
string l = x.substr(0, n/2), r = x.substr(n/2);
left[l].push_back(r);
right[r].push_back(l);
}
dict.erase(start);
dict.erase(end);
unordered_set<string> q0{start}, q1{end};
while (! q0.empty()) {
if (q0.size() > q1.size()) {
swap(q0, q1);
continue;
}
d++;
unordered_set<string> next;
for (auto &x: q0) {
string l = x.substr(0, n/2), r = x.substr(n/2);
if (left.count(l))
for (auto &y: left[l])
if (hamming_one(r, y)) {
string z = l+y;
if (q1.count(z))
return d;
if (dict.count(z)) {
dict.erase(z);
next.insert(z);
}
}
if (right.count(r))
for (auto &y: right[r])
if (hamming_one(l, y)) {
string z = y+r;
if (q1.count(z))
return d;
if (dict.count(z)) {
dict.erase(z);
next.insert(z);
}
}
}
q0.swap(next);
q0.swap(q1);
}
return 0;
}
};