[LeetCode269]Alien Dictionary

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

For example,
Given the following words in dictionary,
[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]
The correct order is: "wertf".

Note:
    You may assume all letters are in lowercase.
    If the order is invalid, return an empty string.
    There may be multiple valid order of letters, return any one of them is fine.
Hide Company Tags Google Facebook
Hide Tags Graph Topological Sort
Hide Similar Problems (M) Course Schedule II

提示是topological sort。利用topological sort can find directed acyclic graph's order that’s why this question can be solved by it.

读题后发现,给我们的dictionary 是sorted by lexicographically according to new alien rules.所以第一排的order都在第二排之前。每一排前面的都在后面的之前。

于是可以把每个char看作node,第一步我们要做的就是把每一排connect。比如wrt to wrf, we only need to add a directed path from t to f.

所以利用BFS把node都放进map里,然后计算每个点的indegree, 然后找order。

比如make graph 完我没有了如下的图:
directed graph w/o cycle

该图的indegree:
这里写图片描述

code 如下:

class Solution {
public:
    string alienOrder(vector<string>& words) {
        if(words.size() == 1) return words[0];
        graph g = makeGraph(words);
        unordered_map<char, int> indegree = computeIndegree(g);
        string res;
        queue<char> toVisit;
        int charNumb = indegree.size();
        //find root!
        for(auto neigh : indegree) {
            if (!neigh.second) {
                toVisit.push(neigh.first);
            }
        }
        for(int i = 0; i<charNumb; ++i){
            if(toVisit.empty()) return "";// the graph is not connected
            char c = toVisit.front();
            toVisit.pop();
            res += c;
            for(auto neigh : g[c]){
                if(!--indegree[neigh]) toVisit.push(neigh);
            }
        }
        return res;
    }
private:
    typedef unordered_map<char, unordered_set<char>> graph;
    graph makeGraph(vector<string>& words){
        graph g;
        size_t n = words.size();
        for (int i = 1; i<n; ++i) {
            string word1 = words[i-1], word2 = words[i];
            size_t l1 = word1.size(), l2 = word2.size();
            int l = max(l1, l2);
            bool found = false;
            for (int j = 0; j < l; ++j) {
                if(j < l1 && g.find(word1[j]) == g.end()) g[word1[j]] = unordered_set<char>();
                if(j < l2 && g.find(word2[j]) == g.end()) g[word2[j]] = unordered_set<char>();
                if(j < l1 && j < l2 && word1[j] != word2[j] && !found){
                    g[word1[j]].insert(word2[j]);
                    found = true;
                }
            }
        }
        return g;
    }
    unordered_map<char, int> computeIndegree(graph& gra){
        unordered_map<char, int> indegree(gra.size());
        for(auto& g : gra){
            if(indegree.find(g.first) == indegree.end()) indegree[g.first] = 0;
            // if (indegree[g.first] == 0) {};//to make sure that a node with 0 indegree will not be left out.
            for(char neigh : g.second) ++indegree[neigh];
        }
        return indegree;
    }
};
发布了109 篇原创文章 · 获赞 0 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章