1. 程式人生 > >[LeetCode] 269. Alien Dictionary

[LeetCode] 269. Alien Dictionary

Problem

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.

Example 1:

Input:

[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

Output: "wertf"

Example 2:

Input:

[
  "z",
  "x"
]

Output: "zx"

Example 3:

Input:

[
  "z",
  "x",
  "z"
] 

Output: ""

Explanation: The order is invalid, so return "".Note:

You may assume all letters are in lowercase.You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.If the order is invalid, return an empty string.There may be multiple valid order of letters, return any one of them is fine.

Solution

class Solution {
    public String alienOrder(String[] words) {
        String res = "";
        if (words == null || words.length == 0) return res;
        
        Map<Character, Integer> degree = new HashMap<>();
        for (String word: words) {
            for (char ch: word.toCharArray()) {
                degree.put(ch, 0);
            }
        }
        
        Map<Character, Set<Character>> map = new HashMap<>();
        for (int i = 0; i < words.length-1; i++) {
            String cur = words[i];
            String next = words[i+1];
            int len = Math.min(cur.length(), next.length());
            for (int j = 0; j < len; j++) {
                char ch1 = cur.charAt(j);
                char ch2 = next.charAt(j);
                if (ch1 != ch2) {
                    
//                     if (map.containsKey(ch1)) {
//                         Set<Character> set = map.get(ch1);
//                         if (!set.contains(ch2)) {
//                             set.add(ch2);
//                             degree.put(ch2, degree.get(ch2)+1);
//                         }
//                     } else {
//                         Set<Character> set = new HashSet<>();
//                         set.add(ch2);
//                         map.put(ch1, set);
//                         degree.put(ch2, degree.get(ch2)+1);
//                     }
//                     break;
                    
                    Set<Character> set = new HashSet<>();
                    if (map.containsKey(ch1)) set = map.get(ch1);
                    if (!set.contains(ch2)) {
                        set.add(ch2);
                        map.put(ch1, set);
                        degree.put(ch2, degree.get(ch2)+1);
                    }
                    break;
                }
            }
        }
        
        Queue<Character> queue = new LinkedList<>();
        for (Map.Entry<Character, Integer> entry: degree.entrySet()) {
            if (entry.getValue() == 0) queue.add(entry.getKey());
        }
        
        while (!queue.isEmpty()) {
            char parent = queue.poll();
            res += parent;
            if (map.containsKey(parent)) {
                for (char child: map.get(parent)) {
                    degree.put(child, degree.get(child)-1);
                    if (degree.get(child) == 0) queue.offer(child);
                }
            }
        }
        
        if (res.length() != degree.size()) return "";
        return res;
    }
}