|
| 1 | +package graph; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Description: https://leetcode.com/problems/alien-dictionary |
| 7 | + * Difficulty: Hard |
| 8 | + * Time complexity: O(n) |
| 9 | + * Space complexity: O(n) |
| 10 | + */ |
| 11 | +public class AlienDictionary { |
| 12 | + |
| 13 | + public String alienOrderViaTopologicalSort(String[] words) { |
| 14 | + Map<Character, List<Character>> adjList = buildAdjList(words); |
| 15 | + if (adjList.isEmpty()) return ""; |
| 16 | + |
| 17 | + // topologically sorted (DESC) graph nodes |
| 18 | + StringBuilder sortedChars = new StringBuilder(); |
| 19 | + int[] visited = new int[26]; |
| 20 | + for (char c : adjList.keySet()) { |
| 21 | + if (visited[c - 'a'] == 0 && hasCycle(c, adjList, visited, sortedChars)) { |
| 22 | + // cycle found -> no valid answer |
| 23 | + return ""; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return sortedChars.reverse().toString(); |
| 28 | + } |
| 29 | + |
| 30 | + private boolean hasCycle( |
| 31 | + char start, |
| 32 | + Map<Character, List<Character>> adjList, |
| 33 | + int[] visited, |
| 34 | + StringBuilder sortedChars) { |
| 35 | + Deque<Character> stack = new LinkedList<>(); |
| 36 | + stack.push(start); |
| 37 | + |
| 38 | + while (!stack.isEmpty()) { |
| 39 | + char current = stack.pop(); |
| 40 | + if (visited[current - 'a'] == 0) { |
| 41 | + visited[current - 'a'] = 1; |
| 42 | + stack.push(current); |
| 43 | + |
| 44 | + for (char neighbor : adjList.getOrDefault(current, List.of())) { |
| 45 | + if (visited[neighbor - 'a'] == 1) return true; |
| 46 | + if (visited[neighbor - 'a'] == 0) { |
| 47 | + stack.push(neighbor); |
| 48 | + } |
| 49 | + } |
| 50 | + } else if (visited[current - 'a'] == 1) { |
| 51 | + visited[current - 'a'] = 2; |
| 52 | + sortedChars.append(current); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + private Map<Character, List<Character>> buildAdjList(String[] words) { |
| 60 | + Map<Character, List<Character>> adjList = new HashMap<>(); |
| 61 | + |
| 62 | + // find all nodes |
| 63 | + for (String word : words) { |
| 64 | + for (char c : word.toCharArray()) { |
| 65 | + adjList.putIfAbsent(c, new ArrayList<>()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // find all edges |
| 70 | + for (int i = 1; i < words.length; i++) { |
| 71 | + String word1 = words[i - 1]; |
| 72 | + String word2 = words[i]; |
| 73 | + |
| 74 | + // prefix goes after longer word -> invalid sequence |
| 75 | + if (word2.length() < word1.length() && word1.startsWith(word2)) return Map.of(); |
| 76 | + |
| 77 | + for (int j = 0; j < Math.min(word1.length(), word2.length()); j++) { |
| 78 | + // find first non match |
| 79 | + if (word1.charAt(j) != word2.charAt(j)) { |
| 80 | + adjList.get(word1.charAt(j)).add(word2.charAt(j)); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return adjList; |
| 87 | + } |
| 88 | +} |
0 commit comments