题目:

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, wherewords 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.

链接: http://leetcode.com/problems/alien-dictionary/

题解:

这道题也是卡了很久,原因在于题意理解不明确。方法还是Topological Sorting,这题的意思是 - Words are sorted, 跟每个Word里面的letter顺序并没有什么关系。就比如"apple"排在"banana"前面,但并不是"apple"里面的"a"就排在"p"前面。所以之前花了不少时间根据每个单词内部的顺序构造edges,结果都是错的。 这种东西归根结底还是我自己的英语不好。写了一大堆总是通不过,发现题目真正含义以后就好很多了,以后面试的话也要多交流多沟通,免得题意弄不清楚白浪费时间做题。另外,对于图来说,图的三种表达方式, list of edges, ajacency matrix和ajacency lists一定要搞清楚,多弄明白。

Kahn's Algorithm: 先把输入words转化为Adjacency Lists或者list of edges,然后使用Course Schedule II的方法进行Topological Sorting。这里我们使用Kahn's Algorithm,先计算inDegree,然后维护一个Queue来进行BFS。

Time Complexity - O(VE),Space Complexity - O(VE)

public class Solution {
public String alienOrder(String[] words) { // Topological sorting - Kahn's Algorithm
if(words == null || words.length == 0) {
return "";
} Map<Character, HashSet<Character>> map = new HashMap<>();
Map<Character, Integer> inDegree = new HashMap<>(); for(String s : words) {
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
inDegree.put(c, 0);
}
} for(int i = 1; i < words.length; i++) { // find (prevChar, curChar) pairs as edges
String prevStr = words[i - 1];
String curStr = words[i];
int len = Math.min(prevStr.length(), curStr.length());
for(int j = 0; j < len; j++) {
char curChar = curStr.charAt(j);
char prevChar = prevStr.charAt(j);
if(curChar == prevChar) {
continue;
} else { // find edges;
if(map.containsKey(prevChar)) {
if(!map.get(prevChar).contains(curChar)) {
map.get(prevChar).add(curChar);
inDegree.put(curChar, inDegree.get(curChar) + 1);
}
} else {
HashSet<Character> tmpSet = new HashSet<>();
tmpSet.add(curChar);
map.put(prevChar, tmpSet);
inDegree.put(curChar, inDegree.get(curChar) + 1);
}
break;
}
}
} Queue<Character> queue = new LinkedList<>();
for(char c : inDegree.keySet()) {
if(inDegree.get(c) == 0) {
queue.offer(c);
}
} StringBuilder res = new StringBuilder();
while(!queue.isEmpty()) {
char c = queue.poll();
res.append(c);
if(map.containsKey(c)) {
for(char l : map.get(c)) {
inDegree.put(l, inDegree.get(l) - 1);
if(inDegree.get(l) == 0) {
queue.offer(l);
}
}
}
} if(res.length() != inDegree.size()) {
return "";
} return res.toString();
}
}

题外话:

这几天食物过于丰富。周五BCD豆腐,周六刘一手火锅(改名了),周日又去了法拉盛吃老周全羊馆,结果就懈怠了。不过和小伙伴们玩玩三国杀放松一下也是很好的。房子的装修也接近尾声,明天周一估计就可以收工了,希望一切顺利。 同时,明天也开始为期一周的培训,主要是Modern Web Development,讲JavaScript的。同时Coursera也有一门讲AngularJS的课程,香港科技大学开设的,明天开始第一天。要趁这个机会好好学一些AngularJS。

二刷:

简化了一下。思路不那么乱了。有几个特殊的点要注意:

  1. 输入中只含有26个小写字母,所以我们可以可以用一个int[26]来计算inDegree
  2. 输出的时候只输出在words里出现过的字符,所以先把字符保存在一个set里。 其实这里也可以和map一起遍历,但需要不少额外的边界条件
  3. 注意单词内部的顺序没有关系,只有单词和前一个单词相同位置不同字间才有先后顺序。
  4. 要注意图的adjancency lists表示结构, 使用Map<Character, Set<Character>>可能会比 Map<Character, List<Character>>快。同时要注意这道题里key保存的是先输出的字符,value里面保存的是后输出的字符。inDegree里也是后输出字符的value被增加
  5. 结尾的时候假如sb.length() < set.size(),说明无法组成一个topological order,我们返回"",否则返回一个topological sorted string   sb.toString();

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();
}
}

Reference:

https://leetcode.com/discuss/53997/the-description-is-wrong

https://leetcode.com/discuss/71991/8ms-clean-java-using-topological-sort-and-dfs

https://leetcode.com/discuss/65274/java-solution-basic-topological-sort

https://leetcode.com/discuss/69894/fastest-java-solution-topological-comments-improvements

https://leetcode.com/discuss/54002/simple-idea-based-on-dfs-4ms-in-c

https://leetcode.com/discuss/54549/java-toposort-solution-clean

https://leetcode.com/discuss/54188/16-18-lines-python-29-lines-c

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. 269. Alien Dictionary 另类字典 *HARD*

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

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

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

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

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

  5. [LeetCode] 269. Alien Dictionary 外文字典

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

  6. LeetCode 269. Alien Dictionary

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

  7. [Locked] Alien Dictionary

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

  8. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  9. Verifying an Alien Dictionary

    2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...

随机推荐

  1. 水王ID查找

    一. 题目 1 三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖数目超过了帖子数目的一半. ...

  2. Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)

    用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...

  3. 深入浅出谈4G ─ 4G LTE网速到底有多快?

    常说4G网速能达100MHz,实际感受远远没有这么快.今天和大家一起算算帐,算算4G LTE网速到底有多快. 基本概念1:资源粒子 个资源粒子就是用个子载波传送个OFDM符号. 1个子载波的带宽是15 ...

  4. 使用Provider时提示:Unable to get provider...

    具体原因还不清楚,只是找到了原因: 这是因为自定义的Provider放的包路径不对,自定义的Provider应该放到和MainActivity同一个包中.

  5. Configuration Management小结

    一.git branch和patch区别 patch,只是把diff部分创建一个分支.Detail: http://www.cnblogs.com/y041039/articles/2411600.h ...

  6. 【翻译】Sencha Touch2.4 The Layout System 布局

    [翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...

  7. 手写PE文件(一)

    DOS Header(IMAGE_DOS_HEADER)->64 Byte DOS头部 DOS Stub 112字节 "PE"00(Signature) 4个字节 IMAGE ...

  8. MX记录

    是邮件交换记录,它指向一个邮件服务器,用于电子邮件系统发邮件时根据 收信人的地址后缀来定位邮件服务器.MX记录也叫做邮件路由记录,用户可以将该域名下的邮件服务器指向到自己的mail server上,然 ...

  9. SOA之(1)——SOA架构基础概念

    在深入探讨什么是面向服务的架构(SOA)之前,先建立一些基本的概念和术语的基本描述而非严格定义,所以也许有些定义在业内还存留争议,此处暂且忽略. 架构基础 技术架构(Technology Archit ...

  10. 利用Openvswitch实现不同物理机中的Docker容器互连

    1. 测试环境 75机(10.11.150.75):Red Hat Enterprise Linux Server 7.0,无外网访问权限,已安装Docker Server 74机(10.11.150 ...