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的更多相关文章

  1. [Locked] Alien Dictionary

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

  2. 【Leetcode_easy】953. Verifying an Alien Dictionary

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

  3. Verifying an Alien Dictionary

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

  4. [LeetCode] Alien Dictionary 另类字典

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

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

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

  6. LeetCode Alien Dictionary

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

  7. Leetcode: Alien Dictionary && Summary: Topological Sort

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

  8. 269. Alien Dictionary

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

  9. [Swift]LeetCode269. 外星人词典 $ Alien Dictionary

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

随机推荐

  1. [Asp.Net]获取客户端ip和mac地址

    摘要 有时候,我们需要获取客户端的一些信息,以便进行统计.比如:客户端的唯一标识,ip等信息 IP 通过获取HTTP_X_FORWARDED_FOR,或者REMOTE_ADDR可以获取客户端的ip. ...

  2. JavaScript日期集合(今日,昨日,本周一,周末 ,月初,月末)

    闲聊:新年第一天上班,看着自己15年年底写的代码,真心觉得很烂,因为年底没时间去写,一想着做后台管理需要获取一周的开始和结束日期,就慌了,项目赶着测试呢,还有好多事情未做,就直接抄袭了网上的一段错误代 ...

  3. 使用MVVM框架(avalonJS)进行快速开发

    背景 在运营活动开发中,因为工作的重复性很大,同时往往开发时间短,某些情况下也会非常紧急,导致了活动开发时间被大大压缩,同时有些活动逻辑复杂,数据或者状态变更都需要手动渲染,容易出错,正是因为这些问题 ...

  4. [译]Mongoose指南 - 中间件

    中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...

  5. VTK初学一,c_Line_CellArray线段的CellArray绘制

    VTK窗口默认坐标方向: #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE ...

  6. R语言练习(二)

    op <- par(mfrow = c(2, 2)) #设置画布 p2 <- curve(x^2, 0, 1) #绘制曲线 legend("topleft", inse ...

  7. 2015多校.Zero Escape (dp减枝 && 滚动数组)

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  8. Timestamp 使用

    Timestamp是一个长整形的类型 1.使用方法一 Timestamp nowdate1 = new Timestamp(System.currentTimeMillis()); System.ou ...

  9. JAVA-多屏幕显示

    以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...

  10. DAY2 Python 标准库 -> Getpass 模块 -> 命令行下输入密码的方法.

    getpass 模块 getpass 模块提供了平台无关的在命令行下输入密码的方法. getpass(prompt) 会显示提示字符串, 关闭键盘的屏幕反馈, 然后读取密码. 如果提示参数省略, 那么 ...