line 12: I was thinking that all the words should follow the same rule pair by pair. So I did two for loops in first place. However, it seems not. This is a typical question that need to be clarified in an interview.
line 13-14: I didn't have these two rows in first place. However, it turns out to be critical otherwise you'll miss those isolated vertices nodes in the graph. For example "wrf" "e". If you don't have these two line, you'll miss "r" and "f" these two nodes.
line 11 & 15: the found variable is really needed. I broke out the second for loop once I found the first different letter in two words. However, that’ll miss the other present letters in the rest of two words.
1: class Solution { 2: public: 3: string alienOrder(vector<string>& words) { 4: if (words.size() == 0) return ""; 5: if (words.size() == 1) return words[0]; 6: // build graph 7: unordered_map<char, set<char>> graph; 8: for (int i = 0; i+1 < words.size(); i++) { 9: string word1 = words[i]; 10: string word2 = words[i+1]; 11:
bool found = false;
12:
for (int j = 0; j < max(word1.size(), word2.size()); j++)
{ 13:
if (j < word1.size() && graph.find(word1[j]) == graph.end()) graph[word1[j]] = set<char>();
14:
if (j < word2.size() && graph.find(word2[j]) == graph.end()) graph[word2[j]] = set<char>();
15: if (j < word1.size() && j < word2.size() && word1[j] != word2[j]
&& !found
) { 16: graph[word1[j]].insert(word2[j]); 17: found = true; 18: } 19: } 20: } 21: // start topology sort 22: vector<bool> visited(26, false); 23: vector<bool> path(26, false); 24: string res; 25: for (auto it = graph.begin(); it != graph.end(); it++) { 26: if (!visited[it->first-'a'] && hasCycle(graph, it->first, visited, path, res)) return ""; 27: } 28: reverse(res.begin(), res.end()); 29: return res; 30: } 31: bool hasCycle(unordered_map<char, set<char>> &graph, char c, vector<bool> &visited, vector<bool> &path, string &res) { 32: if (visited[c-'a']) return false; 33: visited[c-'a'] = path[c-'a'] = true; 34: for (auto it = graph[c].begin(); it != graph[c].end(); it++) { 35: if (path[*it-'a'] || hasCycle(graph, *it, visited, path, res)) return true; 36: } 37: path[c-'a'] = false; 38: res += c; 39: return false; 40: } 41: };
No comments:
Post a Comment