Skip to content

Commit 3d06d82

Browse files
authored
alien_dictionary.cpp
published c++ code fails for this test case: ["zy","zx"]
1 parent 1587b94 commit 3d06d82

File tree

1 file changed

+67
-55
lines changed

1 file changed

+67
-55
lines changed

cpp/neetcode_150/12_advanced_graphs/alien_dictionary.cpp

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,88 @@
99
Space: O(n)
1010
*/
1111

12-
class Solution {
13-
public:
14-
string alienOrder(vector<string>& words) {
15-
unordered_map<char, unordered_set<char>> graph;
12+
// create graph or adjacency list
13+
unordered_map<char, unordered_set<char>> graph; // set to avoid duplicacy
1614
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];
15+
16+
for(auto word: words){
17+
for(auto c : word){
2218
indegree[c] = 0;
2319
}
2420
}
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];
21+
22+
for(int i=0; i<words.size()-1; i++){
3023

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]]++;
24+
bool flag = false;
25+
string s1 = words[i];
26+
string s2 = words[i+1];
27+
28+
int len = min(s1.length(), s2.length());
29+
30+
for(int j=0; j<len; j++){
31+
32+
char ch1 = s1[j];
33+
char ch2 = s2[j];
34+
35+
// if the char do not match
36+
if(ch1 != ch2){
37+
38+
unordered_set<char> set;
39+
if(graph.find(ch1) != graph.end()){
40+
41+
set = graph[ch1];
42+
43+
if(set.find(ch2) == set.end()){
44+
set.insert(ch2);
45+
graph[ch1] = set;
46+
indegree[ch2]++;
47+
}
48+
49+
}
50+
else{
51+
set.insert(ch2);
52+
graph[ch1] = set;
53+
indegree[ch2]++;
3954
}
55+
56+
flag = true;
4057
break;
4158
}
42-
43-
if (j == length - 1 && word1.size() > word2.size()) {
44-
return "";
45-
}
4659
}
60+
if(flag == false and (s1.length() > s2.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);
56-
}
63+
queue<char> pq;
64+
65+
for(auto i : indegree){
66+
if(i.second == 0) pq.push(i.first);
5767
}
5868

59-
while (!q.empty()) {
60-
char c = q.front();
61-
q.pop();
62-
result += c;
69+
string ans = "";
70+
int count = 0;
71+
while(pq.size()>0){
6372

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);
73+
auto rem = pq.front();
74+
pq.pop();
75+
76+
ans += rem;
77+
count++;
78+
79+
if(graph[rem].empty()) continue;
80+
81+
if(graph.find(rem) != graph.end()){
82+
unordered_set<char> nbrs = graph[rem];
83+
84+
for(char nbr : nbrs){
85+
indegree[nbr]--;
86+
if(indegree[nbr] == 0){
87+
pq.push(nbr);
88+
}
7489
}
7590
}
7691
}
77-
78-
// check if it's cyclic
79-
if (result.size() < indegree.size()) {
80-
return "";
92+
93+
if(count == indegree.size()){
94+
return ans;
8195
}
82-
return result;
83-
}
84-
};
96+
return "";

0 commit comments

Comments
 (0)