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 ...
随机推荐
- Orchard源码分析(7.1):Routing(路由)相关
概述 关于ASP.NET MVC中路由有两个基本核心作用,一是通过Http请求中的Url参数等信息获取路由数据(RouteData),路由数据包含了area.controller.action的名称等 ...
- primefaces4.0基本教程以及增删改查
最近试着用了用primefaces4.0,准备写一个基本的增删改查以及分页程序,但在写的过程中发现了很多问题,本想通过百度.谷歌解决,但无奈中文资料非常少,笔者在坑中不停的打滚,终于完成了一个有着基本 ...
- JS禁止WEB页面鼠标事件大全
<!--禁止鼠标右键代码-->:<noscript><ifra:<scriptlanguage=javas:<!--:if(window.Event):doc ...
- 必须知道的.net(字段、属性和方法)
1.字段 通常定义为private(封装原则) 2.属性(property) 通常定义为public,表示类的对外成员.具有可读可写属性,通过get和set访问器实现 3.索引器(indexer) C ...
- 关联规则之Aprior算法(购物篮分析)
0.支持度与置信度 <mahout实战>与<机器学习实战>一起该买的记录数占所有商品记录总数的比例——支持度(整体) 买了<mahout实战>与<机器学习实战 ...
- 【C语言入门教程】7.3 结构体指针的定义和引用
C 语言中指针的操作非常灵活,它也能指向结构体变量对结构体变量进行操作.在学习结构指针之前,需要再次加深对指针的认识.声明指针变量时所使用的数据类型修饰符实际上的作用是定义指针访问内存的范围,如果指针 ...
- Aptana 中去掉“Missing semicolon”提醒
打开“窗口”下的“首选项” 然后找到“Aptana Studio”,在其下找到并点击Validation,在右侧窗口找到Javascript Syntax Validator,在下方的Options中 ...
- WordPress文章浏览历史插件
选自:http://www.ludou.org/wordpress-recently-viewed.html 最近有很多网友问我,露兜博客右边栏底部的 您刚刚看过 栏目是怎么实现.其实我也是参考的这篇 ...
- iOS开发——UI基础-UIImage,UIImageView的使用
1.UIImage 创建UIImage的两种方法 UIImage *image = [UIImage imageNamed:imageNmae]; UIImage *image = [UIImage ...
- ACdream1063——平衡树
1.题目大意:让你设计一种数据结构,支持插入一个数,和在这个结构里查询结构中的哪个数和给定的数的异或值最小 2.分析:这个怎么做呢,就是trie树,我们建立一个trie树,把树按01进制存进去,然后在 ...