269 Alien Dictionary

8 篇文章 0 订阅
3 篇文章 0 订阅

1 题目

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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

Example 1:

Input:
[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

Output: "wertf"

Example 2:

Input:
[
  "z",
  "x"
]

Output: "zx"

Example 3:

Input:
[
  "z",
  "x",
  "z"
] 

Output: "" 

Explanation: The order is invalid, so return "".

2 尝试解

2.1 分析

给定一组字符串,该组字符串以一种新的字母序排列,求该字符串组中出现的字母之间的顺序。

相邻两个字符串之间最多只能得到一个顺序关系,即两个字符串第一对同位不同符字符之间的顺序。用节点表示字母,用A到B的有向边表示A的字母序在B之前。该问题转化为求有向图的拓扑排序。

通用解法为每次取该图中,没有入度的节点放入队列中,并且删除所有从该节点出发的有向边,重复此操作,直到全部节点放入队列中。如果存在多个入度为零的节点,任意选其一即可。如果没有入度为零的节点但图不为空,说明图中有环,无解。

2.2 代码

class Solution {
public:
    string alienOrder(vector<string>& words) {
        unordered_map<char,unordered_set<char>> indegree;
        unordered_map<char,unordered_set<char>> outdegree;
        unordered_set<char> saver(words.back().begin(),words.back().end());
        for(int i = 0; i < words.size()-1; i++){
            string cur = words[i], next = words[i+1];
            for(auto letter : cur) saver.insert(letter);
            for(int j = 0; j < cur.size()&& j < next.size();j++){
                if(cur[j] != next[j])
                {
                    outdegree[cur[j]].insert(next[j]);
                    indegree[next[j]].insert(cur[j]);
                    break;
                }
            }
        }
        string result = "";
        while(saver.size()){
            int loop = 1;
            for(auto letter : saver){
                if(indegree.count(letter)==0){
                    loop = 0;
                    result += letter;
                    for(auto follower : outdegree[letter]){
                        indegree[follower].erase(letter);
                        if(indegree[follower].size() == 0)
                            indegree.erase(follower);
                    }
                    saver.erase(letter);
                    break;
                }
            }
            if(loop) return "";
        }
        return result;
    }
};

3 标准解

3.1 分析

利用相邻字符串获取顺序的部分相同,不同在于拓扑排序的实现。上述代码是DFS形式的实现,也可以用BFS实现。

3.2 代码

class Solution {
public:
    string alienOrder(vector<string>& words) {
        if (words.size() == 0) return "";
        unordered_map<char, int> indegree;
        unordered_map<char, unordered_set<char>> graph;
        
        // initialize
        for (int i = 0; i < words.size(); i++) {
            for (int j = 0; j < words[i].size(); j++) {
                char c = words[i][j];
                indegree[c] = 0; 
            }
        }
        
        // build graph and record indegree
        for (int i = 0; i < words.size() - 1; i++) {
            string cur = words[i];
            string nex = words[i + 1];
            int len = min(cur.size(), nex.size());
            for (int j = 0; j < len; j++) {
                if (cur[j] != nex[j]) {
                    unordered_set<char> set = graph[cur[j]];
                    if (set.find(nex[j]) == set.end()) {
                        graph[cur[j]].insert(nex[j]); // build graph
                        indegree[nex[j]]++; // add indegree
                    }
                    break;                        
                }
            }
        }
        
        // topoligical sort
        string ans;
        queue<char> q;
        for (auto& e : indegree) {
            if (e.second == 0) {
                q.push(e.first);
            }
        }
        while(!q.empty()) {
            char cur = q.front();
            q.pop();
            ans += cur;
            
            if (graph[cur].size() != 0) {
                for (auto& e : graph[cur]) {
                    indegree[e]--;
                    if (indegree[e] == 0) {
                        q.push(e);
                    }
                }
            }            
        }
        
        // tell if it is cyclic
        return ans.length() == indegree.size() ? ans : "";
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值