-
Notifications
You must be signed in to change notification settings - Fork 3
/
269.cpp
55 lines (50 loc) · 1.72 KB
/
269.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
class Solution {
public:
string alienOrder(vector<string>& words) {
int n = words.size();
// build graph
vector<vector<int>> graph(26);
vector<int> inDegrees(26, -1);
// encode first one
// edge case: only one word
for (auto c : words[0]) {
if (inDegrees[c - 'a'] == -1) inDegrees[c - 'a'] = 0;
}
for (int i = 1; i < n; i++) {
string prev = words[i - 1];
string post = words[i];
for (auto c : post) {
if (inDegrees[c - 'a'] == -1) inDegrees[c - 'a'] = 0;
}
int len = min(prev.size(), post.size());
// edge case: prev > post (e.g., "bcda", "bcd")
if (prev.substr(0, len) == post.substr(0, len) && prev.size() > post.size()) return "";
for (int k = 0; k < len; k++) {
if (prev[k] != post[k]) {
graph[prev[k] - 'a'].push_back(post[k] - 'a');
inDegrees[post[k] - 'a']++;
break;
}
}
}
// topological sort
string res;
int count = 0;
queue<int> q;
for (int i = 0; i < 26; i++) {
if (inDegrees[i] == 0) q.push(i);
if (inDegrees[i] >= 0) count++;
}
while (!q.empty()) {
int node = q.front();
q.pop();
res.push_back(static_cast<char>(node + 'a'));
for (auto neighbor : graph[node]) {
inDegrees[neighbor]--;
if (inDegrees[neighbor] == 0) q.push(neighbor);
}
}
if (res.size() < count) return "";
return res;
}
};