Skip to content

Commit e543934

Browse files
authored
Merge pull request neetcode-gh#589 from akhilr007/main
updated solution for alien_dictionary.cpp
2 parents ffaa2d4 + 13f5087 commit e543934

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

cpp/neetcode_150/12_advanced_graphs/alien_dictionary.cpp

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,89 @@
1111

1212
class Solution {
1313
public:
14-
string alienOrder(vector<string>& words) {
14+
string alienOrder(vector<string> &words) {
15+
1516
unordered_map<char, unordered_set<char>> graph;
1617
unordered_map<char, int> indegree;
17-
18-
// initialize, find all unique letters
19-
for (int i = 0; i < words.size(); i++) {
20-
for (int j = 0; j < words[i].size(); j++) {
21-
char c = words[i][j];
22-
indegree[c] = 0;
18+
19+
// indegree make all char 0
20+
for(auto word : words){
21+
for(auto c : st){
22+
indegree[c]=0;
2323
}
2424
}
25-
26-
// build graph, record indegree, find all edges
27-
for (int i = 0; i < words.size() - 1; i++) {
28-
string word1 = words[i];
29-
string word2 = words[i + 1];
25+
26+
for(int i=0; i<words.size()-1; i++){
27+
string curr = words[i];
28+
string next = words[i+1];
3029

31-
// find first mismatch & insert into hash maps
32-
int length = min(word1.size(), word2.size());
33-
for (int j = 0; j < length; j++) {
34-
if (word1[j] != word2[j]) {
35-
unordered_set<char> s = graph[word1[j]];
36-
if (s.find(word2[j]) == s.end()) {
37-
graph[word1[j]].insert(word2[j]);
38-
indegree[word2[j]]++;
30+
bool flag = false;
31+
int len = min(curr.length(), next.length());
32+
for(int j=0; j<len; j++){
33+
char ch1 = curr[j];
34+
char ch2 = next[j];
35+
36+
if(ch1 != ch2){
37+
unordered_set<char> set;
38+
39+
if(graph.find(ch1) != graph.end()){
40+
set = graph[ch1];
41+
42+
if(set.find(ch2) == set.end()){
43+
set.insert(ch2);
44+
indegree[ch2]++;
45+
graph[ch1] = set;
46+
}
47+
}
48+
else{
49+
set.insert(ch2);
50+
indegree[ch2]++;
51+
graph[ch1] = set;
3952
}
53+
54+
flag = true;
4055
break;
4156
}
4257

43-
if (j == length - 1 && word1.size() > word2.size()) {
44-
return "";
45-
}
4658
}
59+
60+
if(flag == false and (curr.length() > next.length())) return "";
4761
}
48-
49-
// bfs + topological sort
50-
string result = "";
51-
queue<char> q;
5262

53-
for (auto it = indegree.begin(); it != indegree.end(); it++) {
54-
if (it->second == 0) {
55-
q.push(it->first);
63+
priority_queue<char, vector<char>, greater<char>> q;
64+
65+
for(auto it : indegree){
66+
if(it.second == 0){
67+
//cout<<it.first<<endl;
68+
q.push(it.first);
5669
}
5770
}
5871

59-
while (!q.empty()) {
60-
char c = q.front();
72+
int count=0;
73+
string ans = "";
74+
75+
while(q.size()>0){
76+
auto rem = q.top();
6177
q.pop();
62-
result += c;
6378

64-
if (graph[c].empty()) {
65-
continue;
66-
}
67-
68-
unordered_set<char> edges = graph[c];
69-
for (auto it = edges.begin(); it != edges.end(); it++) {
70-
char edge = *it;
71-
indegree[edge]--;
72-
if (indegree[edge] == 0) {
73-
q.push(edge);
79+
ans += rem;
80+
count++;
81+
82+
if(graph.find(rem) != graph.end()){
83+
unordered_set<char> nbrs = graph[rem];
84+
85+
for(auto nbr : nbrs){
86+
indegree[nbr]--;
87+
if(indegree[nbr] == 0){
88+
q.push(nbr);
89+
}
7490
}
7591
}
7692
}
77-
78-
// check if it's cyclic
79-
if (result.size() < indegree.size()) {
80-
return "";
93+
94+
if(count == indegree.size()){
95+
return ans;
8196
}
82-
return result;
97+
return "";
8398
}
8499
};

0 commit comments

Comments
 (0)