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

题解:

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((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 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5278208.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值