|
11 | 11 |
|
12 | 12 | class Solution {
|
13 | 13 | public:
|
14 |
| - string alienOrder(vector<string>& words) { |
| 14 | + string alienOrder(vector<string> &words) { |
| 15 | + |
15 | 16 | unordered_map<char, unordered_set<char>> graph;
|
16 | 17 | 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; |
23 | 23 | }
|
24 | 24 | }
|
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]; |
30 | 29 |
|
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; |
39 | 52 | }
|
| 53 | + |
| 54 | + flag = true; |
40 | 55 | break;
|
41 | 56 | }
|
42 | 57 |
|
43 |
| - if (j == length - 1 && word1.size() > word2.size()) { |
44 |
| - return ""; |
45 |
| - } |
46 | 58 | }
|
| 59 | + |
| 60 | + if(flag == false and (curr.length() > next.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); |
| 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); |
56 | 69 | }
|
57 | 70 | }
|
58 | 71 |
|
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(); |
61 | 77 | q.pop();
|
62 |
| - result += c; |
63 | 78 |
|
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 | + } |
74 | 90 | }
|
75 | 91 | }
|
76 | 92 | }
|
77 |
| - |
78 |
| - // check if it's cyclic |
79 |
| - if (result.size() < indegree.size()) { |
80 |
| - return ""; |
| 93 | + |
| 94 | + if(count == indegree.size()){ |
| 95 | + return ans; |
81 | 96 | }
|
82 |
| - return result; |
| 97 | + return ""; |
83 | 98 | }
|
84 | 99 | };
|
0 commit comments