269. Alien Dictionary

本质上就是topological sort.

1. 统计所有的点

  对于每一个string,把所有的字母都加入indegree

2. 构建图,统计indegree

  对于没连续的一组str,找到第一个不同的字母,把后一个加入前一个字母的neighbor,并且给后一个字母indegree+1.

  需要注意的是,如果要检查后一个字母是不是已经在第一个字母的neighbor,如果不是再做后续操作

3. 遍历图

  注意可能没有邻节点

4. 检查是不是所有点都访问到了,返回结果

 1     public String alienOrder(String[] words) {
 2         if(words.length < 1) {
 3             return "";
 4         }
 5         String res = "";
 6         Map<Character, Set<Character>> map = new HashMap<Character, Set<Character>>();
 7         Map<Character, Integer> indegree = new HashMap<Character, Integer>();
 8         for(String s: words) {
 9             for(char c: s.toCharArray()) {
10                 indegree.put(c, 0);
11             }
12         }
13         for(int i = 0; i < words.length - 1; i++) {
14             String cur = words[i];
15             String next = words[i + 1];
16             int len = Math.min(cur.length(), next.length());
17             for(int j = 0; j < len; j++) {
18                 char c1 = cur.charAt(j);
19                 char c2 = next.charAt(j);
20                 if(c1 != c2) {
21                     Set<Character> neighbors = map.get(c1);
22                     if(neighbors == null) {
23                         neighbors = new HashSet<Character>();
24                     }
25                     if(!neighbors.contains(c2)) {
26                         neighbors.add(c2);
27                         map.put(c1, neighbors);
28                         indegree.put(c2, indegree.get(c2) + 1);
29                     }
30                     break;
31                 }
32             }
33         }
34         Queue<Character> queue = new LinkedList<Character>();
35         for(Character c: indegree.keySet()) {
36             if(indegree.get(c) == 0) {
37                 queue.offer(c);
38             }
39         }
40         while(!queue.isEmpty()) {
41             Character c = queue.poll();
42             res += c;
43             Set<Character> neighbors = map.get(c);
44             if(neighbors == null) {
45                 continue;
46             }
47             for(Character x: neighbors) {
48                 indegree.put(x, indegree.get(x) - 1);
49                 if(indegree.get(x) == 0) {
50                     queue.offer(x);
51                 }
52             }
53         }
54         return indegree.size() == res.length()? res: "";
55     }
时间: 2024-10-19 04:45:51

269. Alien Dictionary的相关文章

[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

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

[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

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

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

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