|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 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 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 | + For example, |
| 9 | + Given the following words in dictionary, |
| 10 | +
|
| 11 | + [ |
| 12 | + "wrt", |
| 13 | + "wrf", |
| 14 | + "er", |
| 15 | + "ett", |
| 16 | + "rftt" |
| 17 | + ] |
| 18 | + The correct order is: "wertf". |
| 19 | +
|
| 20 | + Note: |
| 21 | + You may assume all letters are in lowercase. |
| 22 | + If the order is invalid, return an empty string. |
| 23 | + There may be multiple valid order of letters, return any one of them is fine. |
| 24 | + */ |
| 25 | +public class _269 { |
| 26 | + |
| 27 | + /**reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs*/ |
| 28 | + public static String alienOrder(String[] words) { |
| 29 | + Map<Character, Set<Character>> map = new HashMap(); |
| 30 | + Map<Character, Integer> degree = new HashMap<>(); |
| 31 | + String result = ""; |
| 32 | + if (words == null || words.length == 0) return result; |
| 33 | + for (String s : words) { |
| 34 | + for (char c : s.toCharArray()) { |
| 35 | + degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry |
| 36 | + //for each letter in the degree map |
| 37 | + } |
| 38 | + } |
| 39 | + for (int i = 0; i < words.length - 1; i++) { |
| 40 | + String cur = words[i]; |
| 41 | + String next = words[i + 1]; |
| 42 | + int length = Math.min(cur.length(), next.length()); |
| 43 | + for (int j = 0; j < length; j++) { |
| 44 | + char c1 = cur.charAt(j); |
| 45 | + char c2 = next.charAt(j); |
| 46 | + if (c1 != c2) { |
| 47 | + Set<Character> set = new HashSet<>(); |
| 48 | + if (map.containsKey(c1)) { |
| 49 | + set = map.get(c1); |
| 50 | + } |
| 51 | + if (!set.contains(c2)) { |
| 52 | + set.add(c2); |
| 53 | + map.put(c1, set); |
| 54 | + degree.put(c2, degree.get(c2) + 1); |
| 55 | + } |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + Queue<Character> queue = new LinkedList<>(); |
| 61 | + for (char c : degree.keySet()) { |
| 62 | + if (degree.get(c) == 0) queue.add(c); |
| 63 | + } |
| 64 | + while (!queue.isEmpty()) { |
| 65 | + char c = queue.remove(); |
| 66 | + result += c; |
| 67 | + if (map.containsKey(c)) { |
| 68 | + for (char c2 : map.get(c)) { |
| 69 | + degree.put(c2, degree.get(c2) - 1); |
| 70 | + if (degree.get(c2) == 0) { |
| 71 | + queue.add(c2); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + if (result.length() != degree.size()) return ""; |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String ... args){ |
| 81 | + System.out.println("Hello world."); |
| 82 | + String[] words = new String[]{"wrt","wrf","er","ett","rftt"}; |
| 83 | + String order = alienOrder(words); |
| 84 | + System.out.print(order); |
| 85 | + } |
| 86 | +} |
0 commit comments