|
1 | 1 | // https://leetcode.com/problems/alien-dictionary
|
2 | | -// T: O() |
3 | | -// S: O() |
| 2 | +// C = length of all words added together |
| 3 | +// T: O(C) |
| 4 | +// S: O(1) |
4 | 5 |
|
5 | | -import java.util.*; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Queue; |
6 | 12 |
|
7 | 13 | public class AlienDictionary {
|
8 | 14 | public String alienOrder(String[] words) {
|
9 | | - final Map<Character, Set<Character>> graph = createGraphFrom(words); |
10 | | - return lexicographicallySmallest(graph); |
11 | | - } |
12 | | - |
13 | | - private static String lexicographicallySmallest(Map<Character, Set<Character>> graph) { |
14 | | - final Map<Character, Integer> inDegree = computeInDegree(graph); |
15 | | - final Queue<Character> queue = new LinkedList<>(); |
16 | | - final StringBuilder builder = new StringBuilder(); |
17 | | - add0InDegreeInQueue(queue, inDegree); |
18 | 15 |
|
19 | | - while (!queue.isEmpty()) { |
20 | | - final char c = queue.poll(); |
21 | | - builder.append(c); |
22 | | - for (char neighbour : graph.getOrDefault(c, new HashSet<>())) { |
23 | | - inDegree.put(neighbour, inDegree.get(neighbour) - 1); |
24 | | - if (inDegree.get(neighbour) == 0) { |
25 | | - queue.add(neighbour); |
26 | | - } |
| 16 | + // Step 0: Create data structures and find all unique letters. |
| 17 | + Map<Character, List<Character>> adjList = new HashMap<>(); |
| 18 | + Map<Character, Integer> counts = new HashMap<>(); |
| 19 | + for (String word : words) { |
| 20 | + for (char c : word.toCharArray()) { |
| 21 | + counts.put(c, 0); |
| 22 | + adjList.put(c, new ArrayList<>()); |
27 | 23 | }
|
28 | | - graph.remove(c); |
29 | 24 | }
|
30 | 25 |
|
31 | | - if (graph.isEmpty()) { |
32 | | - return builder.toString(); |
33 | | - } |
34 | | - return ""; |
35 | | - } |
36 | | - |
37 | | - private static Map<Character, Set<Character>> createGraphFrom(String[] words) { |
38 | | - final Map<Character, Set<Character>> graph = new HashMap<>(); |
39 | | - addAllLettersInGraph(graph, words); |
40 | | - for (int i = 0 ; i < words.length ; i++) { |
41 | | - for (int j = i + 1 ; j < words.length ; j++) { |
42 | | - if (words[i].equals(words[j])) { |
43 | | - continue; |
44 | | - } |
45 | | - final char[] charDiff = firstDifferentCharacter(words[i], words[j]); |
46 | | - if (charDiff.length == 0) { |
47 | | - continue; |
48 | | - } |
49 | | - final Set<Character> set = graph.getOrDefault(charDiff[0], new HashSet<>()); |
50 | | - set.add(charDiff[1]); |
51 | | - graph.putIfAbsent(charDiff[0], set); |
| 26 | + // Step 1: Find all edges. |
| 27 | + for (int i = 0; i < words.length - 1; i++) { |
| 28 | + String word1 = words[i]; |
| 29 | + String word2 = words[i + 1]; |
| 30 | + // Check that word2 is not a prefix of word1. |
| 31 | + if (word1.length() > word2.length() && word1.startsWith(word2)) { |
| 32 | + return ""; |
52 | 33 | }
|
53 | | - } |
54 | | - return graph; |
55 | | - } |
56 | | - |
57 | | - private static void addAllLettersInGraph(Map<Character, Set<Character>> graph, String[] words) { |
58 | | - for (String word : words) { |
59 | | - for (int i = 0 ; i < word.length() ; i++) { |
60 | | - graph.putIfAbsent(word.charAt(i), new HashSet<>()); |
| 34 | + // Find the first non match and insert the corresponding relation. |
| 35 | + for (int j = 0; j < Math.min(word1.length(), word2.length()); j++) { |
| 36 | + if (word1.charAt(j) != word2.charAt(j)) { |
| 37 | + adjList.get(word1.charAt(j)).add(word2.charAt(j)); |
| 38 | + counts.put(word2.charAt(j), counts.get(word2.charAt(j)) + 1); |
| 39 | + break; |
| 40 | + } |
61 | 41 | }
|
62 | 42 | }
|
63 | | - } |
64 | 43 |
|
65 | | - private static char[] firstDifferentCharacter(String s1, String s2) { |
66 | | - for (int i = 0 ; i < s1.length() && i < s2.length() ; i++) { |
67 | | - if (s1.charAt(i) != s2.charAt(i)) { |
68 | | - return new char[] { s1.charAt(i), s2.charAt(i) }; |
| 44 | + // Step 2: Breadth-first search. |
| 45 | + StringBuilder sb = new StringBuilder(); |
| 46 | + Queue<Character> queue = new LinkedList<>(); |
| 47 | + for (Character c : counts.keySet()) { |
| 48 | + if (counts.get(c).equals(0)) { |
| 49 | + queue.add(c); |
69 | 50 | }
|
70 | 51 | }
|
71 | | - returnnewchar[] {}; |
72 | | - } |
73 | | - |
74 | | - privatestaticMap<Character, Integer> computeInDegree(Map<Character, Set<Character>> graph) { |
75 | | - finalMap<Character, Integer> inDegree = newHashMap<>(); |
76 | | - for (Set<Character> set : graph.values()) { |
77 | | - for (charc : set) { |
78 | | - inDegree.put(c, inDegree.getOrDefault(c, 0) + 1); |
| 52 | + while (!queue.isEmpty()) { |
| 53 | + Characterc = queue.remove(); |
| 54 | +sb.append(c); |
| 55 | + for (Characternext : adjList.get(c)) { |
| 56 | + counts.put(next, counts.get(next) - 1); |
| 57 | + if (counts.get(next).equals(0)) { |
| 58 | + queue.add(next); |
| 59 | + } |
79 | 60 | }
|
80 | 61 | }
|
81 | | - return inDegree; |
82 | | - } |
83 | 62 |
|
84 | | - private static void add0InDegreeInQueue(Queue<Character> queue, Map<Character, Integer> inDegree) { |
85 | | - for (Map.Entry<Character, Integer> entry : inDegree.entrySet()) { |
86 | | - if (entry.getValue() == 0) { |
87 | | - queue.add(entry.getKey()); |
88 | | - } |
| 63 | + if (sb.length() < counts.size()) { |
| 64 | + return ""; |
89 | 65 | }
|
| 66 | + return sb.toString(); |
90 | 67 | }
|
91 | 68 | }
|
0 commit comments