|
| 1 | +\chapter{Graph} |
| 2 | + |
| 3 | +\section{Alien Dictionary} %%%%%%%%%%%%%%%%%%%%%% |
| 4 | + |
| 5 | +\subsubsection{Description} |
| 6 | +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. |
| 7 | + |
| 8 | +\textbf{Example:} |
| 9 | + |
| 10 | +Given the following words in dictionary, |
| 11 | +\begin{Code} |
| 12 | +[ |
| 13 | + "wrt", |
| 14 | + "wrf", |
| 15 | + "er", |
| 16 | + "ett", |
| 17 | + "rftt" |
| 18 | +] |
| 19 | +\end{Code} |
| 20 | + |
| 21 | +The correct order is: \code{"wertf"}. |
| 22 | + |
| 23 | +\textbf{Note:} |
| 24 | + |
| 25 | +1. You may assume all letters are in lowercase. |
| 26 | + |
| 27 | +2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary. |
| 28 | + |
| 29 | +3. If the order is invalid, return an empty string. |
| 30 | + |
| 31 | +4. There may be multiple valid order of letters, return any one of them is fine. |
| 32 | + |
| 33 | +\newpage |
| 34 | + |
| 35 | +\subsubsection{Solution} |
| 36 | + |
| 37 | +\begin{Code} |
| 38 | +public String alienOrder(String[] words) { |
| 39 | + int[] indegree = new int[26]; |
| 40 | + Arrays.fill(indegree, -1); |
| 41 | + |
| 42 | + int count = 0; |
| 43 | + for (String word : words) { |
| 44 | + for (char c : word.toCharArray()) { |
| 45 | + if (indegree[c - 'a'] != 0) { |
| 46 | + indegree[c - 'a'] = 0; |
| 47 | + count++; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + HashMap<Character, Set<Character>> map = new HashMap<>(); |
| 52 | + for (int i = 0; i < words.length - 1; i++) { |
| 53 | + String first = words[i], second = words[i + 1]; |
| 54 | + int len = Math.min(first.length(), second.length()); |
| 55 | + for (int j = 0; j < len; j++) { |
| 56 | + if (first.charAt(j) != second.charAt(j)) { |
| 57 | + Set<Character> set = map.get(first.charAt(j)); |
| 58 | + if (set == null) { |
| 59 | + set = new HashSet<Character>(); |
| 60 | + map.put(first.charAt(j), set); |
| 61 | + } |
| 62 | + if (set.add(second.charAt(j))) { |
| 63 | + indegree[second.charAt(j) - 'a']++; |
| 64 | + } |
| 65 | + break; |
| 66 | + } else { |
| 67 | + if (j + 1 >= second.length() && j + 1 < first.length()) { return ""; } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + Queue<Character> queue = new LinkedList<Character>(); |
| 72 | + for (int i = 0; i < indegree.length; i++) { |
| 73 | + if (indegree[i] == 0) { |
| 74 | + queue.add((char) ('a' + i)); |
| 75 | + } |
| 76 | + } |
| 77 | + StringBuilder sb = new StringBuilder(); |
| 78 | + while (!queue.isEmpty()) { |
| 79 | + Character from = queue.poll(); |
| 80 | + sb.append(from); |
| 81 | + Set<Character> set = map.get(from); |
| 82 | + if (set != null) { |
| 83 | + for (Character to : map.get(from)) { |
| 84 | + if (--indegree[to - 'a'] == 0) { |
| 85 | + queue.add(to); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return sb.length() != count ? "" : sb.toString(); |
| 91 | +} |
| 92 | +\end{Code} |
0 commit comments