private Map<Character, Set<Character>> buildGraph(String words[], Map<Character, Integer> degree, Set<Character> allCharacters) {
        getAllChars(words, degree, allCharacters);
        Set<Character> all = new HashSet<>(allCharacters);
        Map<Character, Set<Character>> graph = new HashMap<>();
        for (int i = 0; i < words.length - 1; i++) {
            String nextWord = words[i + 1];
            for (int k = 0; k < Math.min(words[i].length(), nextWord.length()); k++) {
                if (words[i].charAt(k) != nextWord.charAt((k))) {
                    all.remove(words[i].charAt(k));
                    Set<Character> set = graph.get(words[i].charAt(k));
                    if (set == null) {
                        set = new HashSet<>();
                        graph.put(words[i].charAt(k), set);
                    }
                    set.add(nextWord.charAt(k));
                    degree.compute(nextWord.charAt(k), (key, count) -> count + 1);
                    break;
                }
            }
        }
        for (char ch : all) {
            graph.put(ch, null);
        }
        return graph;
    }
degree is only used for BFS. Not for DFS.
src/com/interview/graph/AlientDictionary.java