LeetCode 269. Alien Dictionary

原题链接在这里:https://leetcode.com/problems/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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

Example 1:
Given the following words in dictionary,

[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

The correct order is: "wertf".

Example 2:
Given the following words in dictionary,

[
  "z",
  "x"
]

The correct order is: "zx".

Example 3:
Given the following words in dictionary,

[
  "z",
  "x",
  "z"
] 

The order is invalid, so return "".

Note:

  1. You may assume all letters are in lowercase.
  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
  3. If the order is invalid, return an empty string.
  4. There may be multiple valid order of letters, return any one of them is fine.

题解:

采用BFS based topological sort. 建立graph 和 indegree array.

字典里有多个单词,这些竖着的单词是按照首字母排序的,如果首字母相同就看第二个字母,以此类推.

用queue把indegree为0的vertex加到queue中开始做BFS.

Note: when using while loop, first thing is to remember to increase index.

Time Complexity: O(V + E). Space: O(V). V最大26. Edge最大为words.length.

AC Java:

 1 class Solution {
 2     public String alienOrder(String[] words) {
 3         if(words == null || words.length == 0){
 4             return "";
 5         }
 6         
 7         //看看words array中都含有哪些字母
 8         HashSet<Character> charSet = new HashSet<>();
 9         for(String w : words){
10             for(char c : w.toCharArray()){
11                 charSet.add(c);
12             }
13         }
14         
15         //构建 adjancy list 形式的graph, 计算每个vertex 的indegree
16         int [] in = new int[26];
17         HashMap<Character, HashSet<Character>> graph = new HashMap<>();
18         for(int i = 1; i < words.length; i++){
19             String pre = words[i - 1];
20             String cur = words[i];
21             int j = 0;
22             while(j < pre.length() && j < cur.length()){
23                 if(pre.charAt(j) != cur.charAt(j)){
24                     char sour = pre.charAt(j);
25                     char dest = cur.charAt(j);
26                     
27                     graph.putIfAbsent(sour, new HashSet<Character>());
28                     if(!graph.get(sour).contains(dest)){
29                         in[dest - 'a']++;
30                     }
31                     
32                     graph.get(sour).add(dest);
33                     break;
34                 }
35                 
36                 j++;
37                 if(j < pre.length() && j == cur.length()){
38                     return "";
39                 }
40             }
41         }
42         
43         //BFS 形式的topologial sort
44         StringBuilder sb = new StringBuilder();
45         LinkedList<Character> que = new LinkedList<>();
46         for(char c = 'a'; c <= 'z'; c++){
47             if(in[c - 'a'] == 0 && charSet.contains(c)){
48                 que.add(c);
49             }
50         }
51         
52         while(!que.isEmpty()){
53             char cur = que.poll();
54             sb.append(cur);
55             if(graph.containsKey(cur)){
56                 for(char c : graph.get(cur)){
57                     in[c - 'a']--;
58                     if(in[c - 'a'] == 0){
59                         que.add(c);
60                     }
61                 }
62             }
63         }
64         
65         //若是sb的length不等于uniqueChar的size, 说明剩下的部分有环
66         return sb.length() == charSet.size() ? sb.toString() : "";
67     }
68 }

类似Course Schedule.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5278208.html