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:

  1. You may assume all letters are in lowercase.
  2. If the order is invalid, return an empty string.
  3. There may be multiple valid order of letters, return any one of them is fine.

给一个单词字典,单词是按照字典序排序,求字母的排序。以题中例子,先看所有单词的第1个字符,可知顺序是w->e-r。然后对于两个连续的单词,找到第一个不相同的字符,比如 wrt和wrf,wr之后t在f之前,所以排序是 t->f。按照当前字母前面出现的字母个数排序,比如w前面有0个字母,e前面有w一个字母,r前面有e和w两个字母,所以排序是w->e->r。因此可以归结为一个拓扑问题,先建图,然后进行遍历。首先统计入度:w的入度是0,e的入度是1,r的入度是2。先把入度为0的节点放入结果中,然后取出w后面连接的节点,将他们的入度-1,如果有入度为0的节点,再放入结果中。

time: 建图->O(n*k), Topological sort-> O(26 + n) = O(n),space: O(n),主要是Map的大小,k表示单词平均长度。

在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列。拓扑排序通常用来“排序”具有依赖关系的任务。

该序列必须满足下面两个条件:1)每个顶点出现且只出现一次。2)若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面。

如何写出它的拓扑排序呢?这里说一种比较常用的方法:

  1. 从 DAG 图中选择一个没有前驱(即入度为0)的顶点并输出。
  2. 从图中删除该顶点和所有以它为起点的有向边。
  3. 重复 1 和 2 直到当前的 DAG 图为空或当前图中不存在无前驱的顶点为止。后一种情况说明有向图中必然存在环。

参考:拓扑排序(Topological Sorting)

Java:

public class Solution {
public String alienOrder(String[] words) { // Topological sorting - Kahn's Algorithm
if(words == null || words.length == 0) {
return "";
}
Map<Character, Set<Character>> graph = new HashMap<>();
Set<Character> set = new HashSet<>();
for (String word : words) {
for (int i = 0; i < word.length(); i++) {
set.add(word.charAt(i));
}
} int[] inDegree = new int[26];
for (int k = 1; k < words.length; k++) {
String preStr = words[k - 1];
String curStr = words[k];
for (int i = 0; i < Math.min(preStr.length(), curStr.length()); i++) {
char preChar = preStr.charAt(i);
char curChar = curStr.charAt(i);
if (preChar != curChar) {
if (!graph.containsKey(preChar)) {
graph.put(preChar, new HashSet<Character>());
}
if (!graph.get(preChar).contains(curChar)) {
inDegree[curChar - 'a']++;
}
graph.get(preChar).add(curChar);
break;
}
}
}
Queue<Character> queue = new LinkedList<>();
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
char c = (char)('a' + i);
if (set.contains(c)) {
queue.offer(c);
}
}
}
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
char c = queue.poll();
sb.append(c);
if (graph.containsKey(c)) {
for (char l : graph.get(c)) {
inDegree[l - 'a']--;
if (inDegree[l - 'a'] == 0) {
queue.offer(l);
}
}
}
}
return sb.length() != set.size() ? "" : sb.toString();
}
}

Python:BFS

class Solution(object):
def alienOrder(self, words):
"""
:type words: List[str]
:rtype: str
"""
result, zero_in_degree_queue, in_degree, out_degree = [], collections.deque(), {}, {}
nodes = sets.Set()
for word in words:
for c in word:
nodes.add(c) for i in xrange(1, len(words)):
if len(words[i-1]) > len(words[i]) and \
words[i-1][:len(words[i])] == words[i]:
return ""
self.findEdges(words[i - 1], words[i], in_degree, out_degree) for node in nodes:
if node not in in_degree:
zero_in_degree_queue.append(node) while zero_in_degree_queue:
precedence = zero_in_degree_queue.popleft()
result.append(precedence) if precedence in out_degree:
for c in out_degree[precedence]:
in_degree[c].discard(precedence)
if not in_degree[c]:
zero_in_degree_queue.append(c) del out_degree[precedence] if out_degree:
return "" return "".join(result) # Construct the graph.
def findEdges(self, word1, word2, in_degree, out_degree):
str_len = min(len(word1), len(word2))
for i in xrange(str_len):
if word1[i] != word2[i]:
if word2[i] not in in_degree:
in_degree[word2[i]] = sets.Set()
if word1[i] not in out_degree:
out_degree[word1[i]] = sets.Set()
in_degree[word2[i]].add(word1[i])
out_degree[word1[i]].add(word2[i])
break  

Python:DFS  

class Solution2(object):
def alienOrder(self, words):
# Find ancestors of each node by DFS.
nodes, ancestors = sets.Set(), {}
for i in xrange(len(words)):
for c in words[i]:
nodes.add(c)
for node in nodes:
ancestors[node] = []
for i in xrange(1, len(words)):
if len(words[i-1]) > len(words[i]) and \
words[i-1][:len(words[i])] == words[i]:
return ""
self.findEdges(words[i - 1], words[i], ancestors) # Output topological order by DFS.
result = []
visited = {}
for node in nodes:
if self.topSortDFS(node, node, ancestors, visited, result):
return "" return "".join(result) # Construct the graph.
def findEdges(self, word1, word2, ancestors):
min_len = min(len(word1), len(word2))
for i in xrange(min_len):
if word1[i] != word2[i]:
ancestors[word2[i]].append(word1[i])
break # Topological sort, return whether there is a cycle.
def topSortDFS(self, root, node, ancestors, visited, result):
if node not in visited:
visited[node] = root
for ancestor in ancestors[node]:
if self.topSortDFS(root, ancestor, ancestors, visited, result):
return True
result.append(node)
elif visited[node] == root:
# Visited from the same root in the DFS path.
# So it is cyclic.
return True
return False

C++:

class Solution {
public:
string alienOrder(vector<string>& words) {
if(words.size() == 0)
return ""; unordered_map<char, vector<char>> d;
unordered_map<char, bool> used; for(auto s : words) {
for(int i = 0; i < s.length(); i++) {
if(used.find(s[i]) == used.end())
used.insert(pair<char, bool>(s[i], false));
}
} for(int i = 1; i < words.size(); i++) {
string cur = words[i];
string pre = words[i - 1];
int j = 0;
while(j < min(cur.length(), pre.length())) {
if(cur[j] != pre[j]) {
if(d.find(pre[j]) == d.end()) {
vector<char> list;
list.push_back(cur[j]);
d.insert(pair<char, vector<char>>(pre[j], list));
}
else {
d[pre[j]].push_back(cur[j]);
}
break;
}
j++;
}
} string result = "";
for(auto it = d.begin(); it != d.end(); it++) {
if(!used[it->first]) {
unordered_set<char> loop;
bool l = topologicalSort(d, used, result, it->first, loop);
if(l)
return "";
}
} for(auto i = used.begin(); i != used.end(); i++) {
if(!i->second)
result = i->first + result;
} return result;
} bool topologicalSort(unordered_map<char, vector<char>> d, unordered_map<char, bool>& used, string& result, char cur, unordered_set<char>& loop) {
used[cur] = true;
loop.insert(cur);
for(auto i : d[cur]) {
if(loop.find(i) != loop.end())
return true;
if(!used[i]) {
bool l = topologicalSort(d, used, result, i, loop);
if(l)
return true;
}
}
result = cur + result;
return false;
}
}; 

   

类似题目:

[LeetCode] 207. Course Schedule 课程安排

[LeetCode] 210. Course Schedule II 课程安排II

All LeetCode Questions List 题目汇总

[LeetCode] 269. Alien Dictionary 外文字典的更多相关文章

  1. [LeetCode] 269. Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  2. [leetcode]269. Alien Dictionary外星字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  3. LeetCode 269. Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...

  4. 269. Alien Dictionary 另类字典 *HARD*

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  5. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  6. 269. Alien Dictionary火星语字典(拓扑排序)

    [抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...

  7. 269. Alien Dictionary

    题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...

  8. [Locked] Alien Dictionary

    Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...

  9. 设计一个 硬件 实现的 Dictionary(字典)

    Dictionary 就是 字典, 是一种可以根据 Key 来 快速 查找 Value 的 数据结构 . 比如 我们在 C# 里用到的 Dictionary<T>, 在 程序设计 里, 字 ...

随机推荐

  1. O(n) 取得数组中每个元素右边最后一个比它大的元素

    题目 2019.9.7,icpc徐州网络赛的E题 XKC's basketball team ,计蒜客上还可以做. 链接:https://nanti.jisuanke.com/t/41387 Inpu ...

  2. new 的对象如何不分配在堆而分配在栈上(方法逃逸等)

    当能够明确对象不会发生逃逸时,就可以对这个对象做一个优化,不将其分配到堆上,而是直接分配到栈上,这样在方法结束时,这个对象就会随着方法的出栈而销毁,这样就可以减少垃圾回收的压力. 如方法逃逸. 逃逸分 ...

  3. python笔记36-装饰器之wraps

    前言 前面一篇对python装饰器有了初步的了解了,但是还不够完美,领导看了后又提出了新的需求,希望运行的日志能显示出具体运行的哪个函数. __name__和doc __name__用于获取函数的名称 ...

  4. golang gomobile 环境搭建

    1. 安装Go语言SDK https://www.golangtc.com/download 2. 配置系统变量这建立GOROOT和GOPATH两个目录,分别对应sdk所在目录与项目文件根目录 3.  ...

  5. forever nodejs管理进程

    何为forever forever可以看做是一个nodejs的守护进程,能够启动,停止,重启我们的app应用.官方的说明是说: A simple CLI tool for ensuring that ...

  6. 项目集成Spring Security

    前言 之前写的 涂涂影院管理系统 这个 demo 是基于 shiro 来鉴权的,项目前后端分离后,显然集成 Spring Security 更加方便一些,毕竟,都用 Spring 了,权限管理当然 S ...

  7. mac系统下 PHPStorm 快捷键

    PHPStorm可以自己设置快捷键 按住command + , 打开Preferences点击Keymap,右边出现下拉框点击下拉框选择你想要的快捷键设置,eclipse快捷键比较常用 eclipse ...

  8. 4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!(转载)

    对于正在运行的mysql,性能如何,参数设置的是否合理,账号设置的是否存在安全隐患,你是否了然于胸呢? 俗话说工欲善其事,必先利其器,定期对你的MYSQL数据库进行一个体检,是保证数据库安全运行的重要 ...

  9. nexus 3.17.0 做为golang 的包管理工具

    nexus 3.17.0 新版本对于go 包管理的支持是基于go mod 的,同时我们也需要一个athens server 然后在nexus 中配置proxy 类型的repo 参考配置 来自官方的配置 ...

  10. 移动端touch触摸事件(滑动效果和手势操作)

    一.定义 ①touch是移动端的触摸事件,而且是一组事件,主要有以下事件: touchstart 事件:当手指触摸屏幕的时候触发 touchmove 事件:当手指在屏幕来回滑动的时候触发 touche ...