[抄题]:

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 "".

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

相同的c2,只需要存一次(没有就新存 有就不存),反正如果存过 就必须退出了(返回一组即可 不用重复加)

"["za","zb","ca","cb"]" How is this test case handled. 
It should give out an empty string as the order can not be decided from the words given. but instead it returns "azbc". 回答:we can only now z-> c and a-> b
so 'azbc' is right but right result is not limited to this only one.
you can test 'zcab', 'abzc' are will all right as it is topological sort

结果字符串长度不等于度数(不是不等于单词数)

["z","z"]的度数 = 字符串长度1
正常,应该返回"z"而不是""

[思维问题]:

忘了拓扑排序用BFS怎么写了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

DFS写拓扑排序似乎都很麻烦

存点到点的对应关系,用map(其中的字符必须存成包装类Character,但是循环的时候可以写char)

        //存每个点的入度
Map<char, Integer> degree = new HashMap<>();
//存c1到c2...的对应关系
Map<char, Set<char>> map = new HashMap<>();

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 出现index[i + 1]时,需要提前备注把上线变为n - 1

[二刷]:

  1. BFS的时候别忘了吧把取出的c1添加到结果中去. 而且必须现有c1的key才能扩展。

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

c1 c2都要先判断一下有没有,再存

[复杂度]:Time complexity: O(n^2 单词数*字母数) Space complexity: O(n)

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

BFS的存储和扩展是两个独立的步骤,扩展时必须先判断key是否存在,再做扩展

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public String alienOrder(String[] words) {
//ini:HashMap<Char, Integer> degree, store all chars into hashmap, String res
//存每个点的入度
Map<Character, Integer> degree = new HashMap<>();
//存c1到c2...的对应关系
Map<Character, Set<Character>> map = new HashMap<>();
String res = "";
for (String word : words) {
for (char c : word.toCharArray())
degree.put(c, 0);
} //compare and store, n - 1
for (int i = 0; i < words.length - 1; i++) {
String cur = words[i];
String next = words[i + 1];
int smallerLen = Math.min(cur.length(), next.length()); for (int j = 0; j < smallerLen; j++) {
char c1 = cur.charAt(j);
char c2 = next.charAt(j); if (c1 != c2) {
//new set
Set<Character> set = new HashSet<>();
//contains c1
if (map.containsKey(c1)) set = map.get(c1); //not contain c2
if (!set.contains(c2)) {
set.add(c2);
map.put(c1, set);
degree.put(c2, degree.get(c2) + 1);
}
break;
}
}
} //bfs, get answer
Queue<Character> q = new LinkedList<>();
for (char c : degree.keySet()) {
if (degree.get(c) == 0) q.offer(c);
} while (!q.isEmpty()) {
char c1 = q.remove();
res += c1;
if (map.containsKey(c1)) {
for (char c2 : map.get(c1)) {
degree.put(c2, degree.get(c2) - 1);
if (degree.get(c2) == 0) q.offer(c2);
}
}
} //cc at end
if (res.length() != degree.size()) return ""; return res;
}
}

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

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

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

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

  5. LeetCode 269. Alien Dictionary

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

  6. 269. Alien Dictionary

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

  7. LeetCode编程训练 - 拓扑排序(Topological Sort)

    拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...

  8. 算法与数据结构基础 - 拓扑排序(Topological Sort)

    拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...

  9. [USACO12DEC]第一!First!(字典树,拓扑排序)

    [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that by changing th ...

随机推荐

  1. bzoj4806 炮

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4806 这种题应该想状压的. 于是发现压不下,结合每一行每一列最多放两个炮想到记一下放炮的列就 ...

  2. JavaFX 之自定义窗口标题栏(二)

    一.问题场景 PC客户端登录界面仿QQ,上边显示图片,下边显示输入框和登录按钮.而JavaFX默认的窗口,不满足需求. 二.解决思路 隐藏窗口默认的标题栏,使用创建label对象,使用css将按钮图片 ...

  3. 如何将angular-ui-bootstrap的图片轮播组件封装成一个指令

    在项目开发中我们经常会遇到图片轮播的功能点: 如果我们开发人员自己原生手写,将会花费很多的时间,最终得不偿失. 接下来就详细说说如何使用angular-ui发热图片轮播模块,并且将它写成一个指令(便于 ...

  4. struts2学习(10)struts2国际化

    一.国际化简介: 二.struts2国际化设置: struts.xml: <?xml version="1.0" encoding="UTF-8" ?&g ...

  5. 杂项-数学软件:Maple

    ylbtech-杂项-数学软件:Maple Maple是目前世界上最为通用的数学和工程计算软件之一,在数学和科学领域享有盛誉,有“数学家的软件”之称.Maple 在全球拥有数百万用户,被广泛地应用于科 ...

  6. [Java.Web] Servlet 的一些细节

    本文来自 传智播客视频PPT 1. 由于客户端是通过 URL 地址访问 web 服务器中的资源,所以 Servlet 程序若想被外界访问,必须把 servlet 程序映射到一个 URL 地址上,这个工 ...

  7. 0908期 HTML form表单

    表单基础摘要 <form id="" name="" method="post/get" action="负责处理的服务端& ...

  8. 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)

    随着OS X 10.9 于 2013年6月10日在旧金山WWDC(world wide developer conference)上发布.是首个不使用猫科动物命名的系统,而转用加利福尼亚的产物. 该系 ...

  9. ubuntu apt-get常用命令

    apt-cache search package 搜索包apt-cache show package 获取包的相关信息,如说明.大小.版本等sudo apt-get install package 安 ...

  10. ubuntu安装Theano+cuda

    由于学习需要用到GPU加速机器学习算法,需要安装theano+cuda. 开源库的一大问题就是:难安装. 为了搞好这个配置,我是前前后后花了3天,重装了3次ubuntu重装了5次驱动才搞定. 故发此贴 ...