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.
分析:
其实解这题的时候应该能够想到有向图。毕竟每个字符之间存在先后顺序。既然能够想到用有向图解,那么怎么按照顺序把每个字符排列出来,就不难想到用Topological Sort 来解决问题了。用Node来表示每个图中的点,并且记录每个点的入度,当入度为0的时候,表明没有其它点指向改点。
public class Solution {
public static String alienOrder(String[] words) {
Map<Character, Node> map = new HashMap<>();
Arrays.stream(words).forEach(word -> {
for (char ch : word.toCharArray()) {
if (!map.containsKey(ch)) {
map.put(ch, new Node(ch));
}
}
});
for (int i = ; i < words.length - ; i++) {
char startChar = ' ', endChar = ' ';
for (int j = ; j < Math.min(words[i].length(), words[i + ].length()); j++) {
if (words[i].charAt(j) != words[i + ].charAt(j)) {
startChar = words[i].charAt(j);
endChar = words[i + ].charAt(j);
break;
}
}
if (startChar != endChar) {
map.get(startChar).neighbour.add(map.get(endChar));
map.get(endChar).degree++;
}
}
// Topological Sort
Queue<Node> queue = new LinkedList<>();
String ans = "";
for (Node node : map.values()) {
if (node.degree == ) {
queue.offer(node);
}
}
while (!queue.isEmpty()) {
Node node = queue.poll();
ans = ans + node.ch;
for (Node neighbour : node.neighbour) {
neighbour.degree--;
if (neighbour.degree == ) {
queue.offer(neighbour);
}
}
}
for (Node node : map.values()) {
if (node.degree != ) {
return "";
}
}
return ans;
}
}
class Node {
public int degree;
public char ch;
public ArrayList<Node> neighbour = new ArrayList<>();
public Node(char ch) {
this.ch = ch;
degree = ;
}
}
Alien Dictionary的更多相关文章
- [Locked] Alien Dictionary
Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- Verifying an Alien Dictionary
2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...
- [LeetCode] Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary 另类字典 *HARD*
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- LeetCode Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- Leetcode: Alien Dictionary && Summary: Topological Sort
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary
题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...
- [Swift]LeetCode269. 外星人词典 $ Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
随机推荐
- EF: Raw SQL Queries
Raw SQL Queries Entity Framework allows you to query using LINQ with your entity classes. However, t ...
- SqlServer 18456 错误解决方案
sqlserver 安装需注意事项: 不要选择,否则运行一段时间后sqlserver登录时会出现18456错误. 发生18456错误(sa 可登录的情况下)解决本地用户登录问题的方案: <1& ...
- Java Programming Test Question 2
public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...
- linux安装软件通常会做哪些事
一般来说,安装某个包,某个服务,某个软件时,可能会做以下事情(不一定全部) - 在安装目录: /usr/bin, /usr/lib: /usr/you_specified_bin/, /usr/you ...
- AngularJS API之equal比较对象
使用情况 1 首先,所有满足 a === 3 这种的对象,在angular.equals(a,b)中都会返回真 2 所有对象的类型,以及属性值都相同的,也会返回真 3 NaN和NaN也会返回真(在ja ...
- iOS 滑动性能优化
iOS 滑动性能优化 目录 一. 减少图层的Blend操作 1. UIView的背景色避免使用clearColor 2. 控件贴图避免使用带alpha的图片 3. UIImageView 使用时避免半 ...
- AlwaysOn可用性组功能测试(一)--AlwaysOn故障转移测试
具体测试环境请参考: AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建 AlwaysOn可用性组测试环境安装与配置(二)--AlwaysOn配置(界面与T-SQL) 一. Alw ...
- Android应用如何监听自己是否被卸载及卸载反馈功能的实现
一个应用被用户卸载肯定是有理由的,而开发者却未必能得知这一重要的理由,毕竟用户很少会主动反馈建议,多半就是用得不爽就卸,如果能在被卸载后获取到用户的一些反馈,那对开发者进一步改进应用是非常有利的.目前 ...
- 【bzoj4034】[HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...
- 分享一个c#t的网页抓取类
using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...