LeetCode(269) Alien Dictionary (Java)

题目如下:

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.

For example,
Given the following words in dictionary,
[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]
The correct order is: "wertf".
Note:
You may assume all letters are in lowercase.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.


分析:

先看上面的例子。

先看所有单词的第0个字符,可以知道先后顺序是w->e-r。

再看第1个字符,先后顺序是r->t,

再看第3个字符,先后顺序是t->f

当有有了若干部分的依赖关系之后,想得到完成的排序,那么可以考虑拓扑排序。虽然听上去很基本,但是代码处理起来有很多细节需要注意。

从这位大神的讨论帖里学到很简洁的C++代码: 先构造合适的依赖关系,然后进行拓扑排序。

我搬运之并修改为Java

public class Solution {
    public String alienOrder(String[] words) {
        if (words == null || words.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        HashMap> suc = new HashMap>();
        HashMap pre = new HashMap ();
        HashSet charSet = new HashSet();
        String previousWord = new String("");
        for (String currentWord : words) {
            for (Character c : currentWord.toCharArray()) {
                charSet.add(c);
            }
            for (int i = 0; i < Math.min(previousWord.length(), currentWord.length()); ++i) {
                if (previousWord.charAt(i) != currentWord.charAt(i)) {
                    if (!pre.containsKey(currentWord.charAt(i))) { //----section_pre----
                        pre.put(currentWord.charAt(i), 1);
                    } else {
                        //ERROR1: 过滤掉重复的关系 ["za","zb","ca","cb"], 从输入得出的中间结论,"a->b"会重复出现两次,需要滤重。为了实现滤重,重新调整了section_pre和section_suc部分的顺序。
                        if (suc.containsKey(previousWord.charAt(i)) && 
                        suc.get(previousWord.charAt(i)).contains(currentWord.charAt(i))) {
                            continue;
                        }
                        pre.put(currentWord.charAt(i), pre.get(currentWord.charAt(i)) + 1);
                    }
                    if (!suc.containsKey(previousWord.charAt(i))) { //----section_suc----
                        suc.put(previousWord.charAt(i), new HashSet ());
                    }
                    suc.get(previousWord.charAt(i)).add(currentWord.charAt(i));                    
                    break; //the BEST part of this question
                }
            }
            previousWord = currentWord;
        }
        Queue queue = new LinkedList();
        for (Character each : charSet) {
            if (!pre.containsKey(each)) {
                queue.add(each);
            }
        }


        while (!queue.isEmpty()) {
            Character currentChar = queue.poll();
            sb.append(currentChar);
            //ERROR2 should check containsKey
            if (!suc.containsKey(currentChar)) {
                continue;
            }
            for (Character each : suc.get(currentChar)) {
                pre.put(each, pre.get(each) - 1);
                if (pre.get(each) == 0) {
                    queue.offer(each);
                }
            }
        }
        return sb.length() == charSet.size() ? sb.toString() : "";
    }
}



你可能感兴趣的:(Java)