1. 程式人生 > >269. Alien Dictionary

269. Alien Dictionary

Description

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

給一組String陣列,其中word按照字典順序排列,找到正確的字母順序。

Using a hash map to char set which contains the characters after a char. Another map contains each char’s degree(priority). Initialize degree first. Then for each char in word in words, construct the map.

Using a linked list queue to perform BFS, push the char with degree 0 into queue. Then in BFS, add peek char to res, then update the degrees of the chars after it, find another char with degree equals to 0, add it in queue for BFS.

If the length of res is compitable with degree.size(), return res, or we should return an empty string.

Code

class Solution {
    public String alienOrder(String[] words) {
        Map<Character, Set<Character>> map = new HashMap<>();
        Map<Character, Integer> degree = new HashMap<>();
        String res = "";
        if (words.length == 0){
            return res;
        }
        for (String word : words){
            for (char ch : word.toCharArray()){
                if (!degree.containsKey(ch)){
                    degree.put(ch, 0);
                }
            }
        }
        for (int i = 0; i < words.length - 1; i++){
            String cur = words[i];
            String next = words[i + 1];
            int length = Math.min(cur.length(), next.length());
            for (int j = 0; j < length; j++){
                char c1 = cur.charAt(j);
                char c2 = next.charAt(j);
                if (c1 != c2){
                    Set<Character> set = map.getOrDefault(c1, new HashSet<Character>());
                    if (!set.contains(c2)){
                        set.add(c2);
                        map.put(c1, set);
                        degree.put(c2, degree.get(c2) + 1);
                    }
                    break;
                }
            }
        }
        Queue<Character> queue = new LinkedList<>();
        for (char c : degree.keySet()){
            if (degree.get(c) == 0){
                queue.add(c);
            }
        }
        while (!queue.isEmpty()){
            char c = queue.remove();
            res += c;
            if (map.containsKey(c)){
                for (char ch : map.get(c)){
                    degree.put(ch, degree.get(ch) - 1);
                    if (degree.get(ch) == 0){
                        queue.add(ch);
                    }
                }
            }
        }
        if (res.length() != degree.size()){
            return "";
        }
        return res;
    }
}

Time Complexity: O() Space Complexity: O()

Review