LeetCode 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 words from the dictionary, wherewords 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:

  1. You may assume all letters are in lowercase.
  2. If the order is invalid, return an empty string.
  3. There may be multiple valid order of letters, return any one of them is fine.

题解:

与Course Schedule相似,采用BFS based topological sort. 建立graph 和 indegree array.

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

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

AC Java:

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

 

你可能感兴趣的:(LeetCode Alien Dictionary)