Advertisement
1988coder

269. Alien Dictionary

Jan 17th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/alien-dictionary/
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Stack;
  6.  
  7. /**
  8.  * Using DFS and topological sort
  9.  *
  10.  * Time Complexity: O(Vertices + Edges). Here number of edges is N-1, where N is
  11.  * the no of words in the input array. Number of vertices equal to the unique
  12.  * number of characters in input array. Thus Time Complexity: O(N * L + (N-1)) =
  13.  * O(N * L). Where N is the average length of each character. In case the
  14.  * character range is a..z then maximum number of unique characters will be 26.
  15.  * In this can the time Complexity will become O(N).
  16.  *
  17.  * Space Complexity: O(Vertices + Edges). Refer to the above explaination of
  18.  * time complexity.
  19.  */
  20. class Solution {
  21.     public String alienOrder(String[] words) {
  22.         if (words == null || words.length == 0) {
  23.             return "";
  24.         }
  25.         if (words.length == 1) {
  26.             return words[0];
  27.         }
  28.  
  29.         HashMap<Character, HashSet<Character>> graph = buildGraph(words);
  30.         HashSet<Character> visited = new HashSet<>();
  31.         HashSet<Character> visiting = new HashSet<>();
  32.         Stack<Character> order = new Stack<>();
  33.  
  34.         for (char c : graph.keySet()) {
  35.             if (!visited.contains(c) && hasCycle(graph, c, visited, visiting, order)) {
  36.                 return "";
  37.             }
  38.         }
  39.  
  40.         StringBuilder sb = new StringBuilder();
  41.         while (!order.isEmpty()) {
  42.             sb.append(order.pop());
  43.         }
  44.  
  45.         return sb.toString();
  46.     }
  47.  
  48.     private HashMap<Character, HashSet<Character>> buildGraph(String[] words) {
  49.         HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  50.  
  51.         for (String s : words) {
  52.             for (char c : s.toCharArray()) {
  53.                 graph.put(c, new HashSet<>());
  54.             }
  55.         }
  56.  
  57.         for (int i = 1; i < words.length; i++) {
  58.             char[] w1 = words[i - 1].toCharArray();
  59.             char[] w2 = words[i].toCharArray();
  60.  
  61.             for (int j = 0; j < Math.min(w1.length, w2.length); j++) {
  62.                 if (w1[j] != w2[j]) {
  63.                     graph.get(w1[j]).add(w2[j]);
  64.                     break;
  65.                 }
  66.             }
  67.         }
  68.  
  69.         return graph;
  70.     }
  71.  
  72.     private boolean hasCycle(HashMap<Character, HashSet<Character>> graph, char c, HashSet<Character> visited,
  73.             HashSet<Character> visiting, Stack<Character> order) {
  74.  
  75.         // (visited.contains(c)) can be removed as it is redundant. Because we are
  76.         // checking this before calling hasCycle function.
  77.         if (visited.contains(c)) {
  78.             return false;
  79.         }
  80.         if (visiting.contains(c)) {
  81.             return true;
  82.         }
  83.  
  84.         visiting.add(c);
  85.  
  86.         for (char n : graph.get(c)) {
  87.             if (!visited.contains(n) && hasCycle(graph, n, visited, visiting, order)) {
  88.                 return true;
  89.             }
  90.         }
  91.  
  92.         visiting.remove(c);
  93.         visited.add(c);
  94.         order.push(c);
  95.         return false;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement