|
9 | 9 | Space: O(n)
|
10 | 10 | */
|
11 | 11 |
|
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 |
16 | 14 | 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){ |
22 | 18 | indegree[c] = 0;
|
23 | 19 | }
|
24 | 20 | }
|
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++){ |
30 | 23 |
|
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]++; |
39 | 54 | }
|
| 55 | + |
| 56 | + flag = true; |
40 | 57 | break;
|
41 | 58 | }
|
42 |
| - |
43 |
| - if (j == length - 1 && word1.size() > word2.size()) { |
44 |
| - return ""; |
45 |
| - } |
46 | 59 | }
|
| 60 | + if(flag == false and (s1.length() > s2.length())) return ""; |
47 | 61 | }
|
48 |
| - |
49 |
| - // bfs + topological sort |
50 |
| - string result = ""; |
51 |
| - queue<char> q; |
52 | 62 |
|
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); |
57 | 67 | }
|
58 | 68 |
|
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){ |
63 | 72 |
|
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 | + } |
74 | 89 | }
|
75 | 90 | }
|
76 | 91 | }
|
77 |
| - |
78 |
| - // check if it's cyclic |
79 |
| - if (result.size() < indegree.size()) { |
80 |
| - return ""; |
| 92 | + |
| 93 | + if(count == indegree.size()){ |
| 94 | + return ans; |
81 | 95 | }
|
82 |
| - return result; |
83 |
| - } |
84 |
| -}; |
| 96 | + return ""; |
0 commit comments