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.
分析:
其实解这题的时候应该能够想到有向图。毕竟每个字符之间存在先后顺序。既然能够想到用有向图解,那么怎么按照顺序把每个字符排列出来,就不难想到用Topological Sort 来解决问题了。用Node来表示每个图中的点,并且记录每个点的入度,当入度为0的时候,表明没有其它点指向改点。
public class Solution {
public static String alienOrder(String[] words) {
Map<Character, Node> map = new HashMap<>();
Arrays.stream(words).forEach(word -> {
for (char ch : word.toCharArray()) {
if (!map.containsKey(ch)) {
map.put(ch, new Node(ch));
}
}
}); for (int i = ; i < words.length - ; i++) {
char startChar = ' ', endChar = ' ';
for (int j = ; j < Math.min(words[i].length(), words[i + ].length()); j++) {
if (words[i].charAt(j) != words[i + ].charAt(j)) {
startChar = words[i].charAt(j);
endChar = words[i + ].charAt(j);
break;
}
}
if (startChar != endChar) {
map.get(startChar).neighbour.add(map.get(endChar));
map.get(endChar).degree++;
}
}
// Topological Sort
Queue<Node> queue = new LinkedList<>();
String ans = "";
for (Node node : map.values()) {
if (node.degree == ) {
queue.offer(node);
}
}
while (!queue.isEmpty()) {
Node node = queue.poll();
ans = ans + node.ch;
for (Node neighbour : node.neighbour) {
neighbour.degree--;
if (neighbour.degree == ) {
queue.offer(neighbour);
}
}
}
for (Node node : map.values()) {
if (node.degree != ) {
return "";
}
}
return ans;
}
} class Node {
public int degree;
public char ch;
public ArrayList<Node> neighbour = new ArrayList<>(); public Node(char ch) {
this.ch = ch;
degree = ;
}
}