[LeetCode] 269. Alien Dictionary

Mars dictionary. Meaning of the questions is to give a bunch of strings, you output (not our normal understanding of the order of 26 letters) real order of the letters between them. example,

Example 1:

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

Output: "wertf"

Example 2:

Input:
[
  "z",
  "x"
]

Output: "zx"

Example 3:

Input:
[
  "z",
  "x",
  "z"
] 

Output: "" 

Explanation: The order is invalid, so return "".

The idea is topological sorting , I was given here BFS 's solution. First, for each of the letters were all involved to a penetration 1, while the need to calculate the total involvement here how many different letters count. After traversing the input again, this time need to create a graph edges. It is a way to create a hashmap, step a few more details of the points below.

After word pairwise comparison, first discovered a different letter, such as this example,

"wrt"和"wrf"

Put t as a key into the hashmap, map the value is a hashset, then f join hashset, but also to the degree of f ++, immediately after the break (between every two words because in fact can only get a maximum of two the relative order between letters). After all do the same traversed words, create a graph based on the degree of the table, or put into a letter of 1 join queue. After you create a graph, again it is like traversing a course schedule BFS each edge, the moment will pop queue traversal characters to append to the result set; at the same time to see whether this character in hashmap, if in, they can go to see his corresponding hashset there is no penetration of the letter 1, if also added queue. Finally, if the length of the result set res count is inconsistent with an empty string is returned.

Time O (V + E)

Space O (n) - the number of words

Java implementation

 1 class Solution {
 2     public static String alienOrder(String[] words) {
 3         if (words == null || words.length == 0)
 4             return "";
 5         StringBuilder res = new StringBuilder();
 6         HashMap<Character, Set<Character>> map = new HashMap<>();
 7         int[] degree = new int[26];
 8         int count = 0;
 9         for (String word : words) {
10             for (char c : word.toCharArray()) {
11                 if (degree[c - 'a'] == 0) {
12                     count++;
13                     degree[c - 'a'] = 1;
14                 }
15             }
16         }
17 
18         for (int i = 0; i < words.length - 1; i++) {
19             char[] cur = words[i].toCharArray();
20             char[] next = words[i + 1].toCharArray();
21             // corner case
22             // ["abc", "ab"]
23             if (cur.length > next.length && words[i].startsWith(words[i + 1])) {
24                 return "";
25             }
26 
27             // normal case, created the graph
28             int len = Math.min(cur.length, next.length);
29             for (int j = 0; j < len; j++) {
30                 if (cur[j] != next[j]) {
31                     if (!map.containsKey(cur[j])) {
32                         map.put(cur[j], new HashSet<>());
33                     }
34                     if (map.get(cur[j]).add(next[j])) {
35                         degree[next[j] - 'a']++;
36                     }
37                     break;
38                 }
39             }
40         }
41 
42         Queue<Character> queue = new LinkedList<>();
43         for (int i = 0; i < 26; i++) {
44             if (degree[i] == 1) {
45                 queue.offer((char) ('a' + i));
46             }
47         }
48 
49         while (!queue.isEmpty()) {
50             char c = queue.poll();
51             res.append(c);
52             if (map.containsKey(c)) {
53                 for (char ch : map.get(c)) {
54                     if (--degree[ch - 'a'] == 1) {
55                         queue.offer(ch);
56                     }
57                 }
58             }
59         }
60         if (res.length() != count) {
61             return "";
62         }
63         return res.toString();
64     }
65 }

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12657598.html