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 来解决问题了。下面的代码来自Jiuzhang. 他用了Node来表示每个图中的点,并且记录每个点的入度,当入度为0的时候,表明没有其它点指向改点。

 1 public class Solution {
 2     public class Node {
 3         public int degree;
 4         public ArrayList<Integer> neighbour = new ArrayList<Integer>();
 5         void Node() {
 6             degree = 0;
 7         }
 8     }
 9     public String alienOrder(String[] words) {
10         Node[] node = new Node[26];
11         boolean[] happen = new boolean[26];
12         for (int i = 0; i < 26; i++) {
13             node[i] = new Node();
14         }
15         //Build the Graph
16         for (int i = 0; i < words.length; i++) {
17             int startPoint = 0, endPoint = 0;
18             for (int j = 0; j < words[i].length(); j++) {
19                 happen[charToInt(words[i].charAt(j))] = true;
20             }
21             if (i != words.length - 1) {
22                 for (int j = 0; j < Math.min(words[i].length(), words[i + 1].length()); j++) {
23                     if (words[i].charAt(j) != words[i + 1].charAt(j)) {
24                         startPoint = charToInt(words[i].charAt(j));
25                         endPoint = charToInt(words[i + 1].charAt(j));
26                         break;
27                     }
28                 }
29             }
30             if (startPoint != endPoint) {
31                 node[startPoint].neighbour.add(endPoint);
32                 node[endPoint].degree++;
33             }
34         }
35         //Topological Sort
36         Queue<Integer> queue = new LinkedList<Integer>();
37         String ans = "";
38         for (int i = 0; i < 26; i++) {
39             if (node[i].degree == 0 && happen[i]) {
40                 queue.offer(i);
41                 ans = ans + intToChar(i);
42             }
43         }
44         while (!queue.isEmpty()) {
45             int now = queue.poll();
46             for (int i : node[now].neighbour) {
47                 node[i].degree--;
48                 if (node[i].degree == 0) {
49                     queue.offer(i);
50                     ans = ans + intToChar(i);
51                 }
52             }
53         }
54         for (int i = 0; i < 26; i++) {
55             if (node[i].degree != 0) {
56                 return "";
57             }
58         }
59         return ans;
60     }
61     public char intToChar(int i) {
62         return (char)(‘a‘ + i);
63     }
64     public int charToInt(char ch) {
65         return ch - ‘a‘;
66     }
67 }
时间: 2024-11-03 05:42:48

Alien Dictionary的相关文章

[Locked] Alien Dictionary

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 lang

leetcode 953. Verifying an Alien Dictionary &amp; 949. Largest Time for Given Digits &amp; 948. Bag of Tokens

leetcode 953. Verifying an Alien Dictionary class Solution { public boolean isAlienSorted(String[] words, String order) { int[] o = new int[26]; for (int i = 0; i < order.length(); ++i) { o[order.charAt(i) - 'a'] = i; } for (int i = 0; i < words.len

Leetcode: Alien Dictionary &amp;&amp; Summary: Topological Sort

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

[LeetCode] 269. 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

leetcode269 - Alien Dictionary - hard

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

[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order 

[LeetCode] Alien Dictionary

Well, this problem is not that easy. First you may need some clarifications about the problem itself. If you do, you may refer to this link for a nice example which illustrates the purpose of this problem. Moreover, you need to understand graph repre

269. Alien Dictionary

本质上就是topological sort. 1. 统计所有的点 对于每一个string,把所有的字母都加入indegree 2. 构建图,统计indegree 对于没连续的一组str,找到第一个不同的字母,把后一个加入前一个字母的neighbor,并且给后一个字母indegree+1. 需要注意的是,如果要检查后一个字母是不是已经在第一个字母的neighbor,如果不是再做后续操作 3. 遍历图 注意可能没有邻节点 4. 检查是不是所有点都访问到了,返回结果 1 public String a

LeetCode 269: Alien Dictionary

Notes: Lots of places need to be remember: Edge cases: 1. Only one word, still need to be calculated as it represents. 2. All unique chars that not be placed in the result means there are several cycles. It must return empty string. Data Structure: 1