
There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you.
You are given a list of strings words from the alien language’s dictionary, where the strings in words are sorted lexicographically by the rules of this new language.
Return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language’s rules. If there is no solution, return “”. If there are multiple solutions, return any of them.
A string s is lexicographically smaller than a string t if at the first letter where they differ, the letter in s comes before the letter in t in the alien language. If the first min(s.length, t.length) letters are the same, then s is smaller if and only if s.length < t.length.
1
2
3
4
| Example 1:
Input: words = ["wrt","wrf","er","ett","rftt"]
Output: "wertf"
|
1
2
3
4
| Example 2:
Input: words = ["z","x"]
Output: "zx"
|
1
2
3
4
5
| Example 3:
Input: words = ["z","x","z"]
Output: ""
Explanation: The order is invalid, so return "".
|
Constraints:
- 1 <= words.length <= 100
- 1 <= words[i].length <= 100
- words[i] consists of only lowercase English letters.
Solution BFS + Topological Sort#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| class Solution {
public String alienOrder(String[] words) {
Map<Character, List<Character>> g = new HashMap<>();
Map<Character, Integer> inDegree = new HashMap();
for (String word: words) {
for (char c : word.toCharArray()) {
g.put(c, new ArrayList<>());
inDegree.put(c, 0);
}
}
for (int i = 1; i < words.length; i++) {
String w1 = words[i-1];
String w2 = words[i];
if (w1.startsWith(w2) && w1.length() > w2.length()) {
return "";
}
for (int j = 0; j < Math.min(w1.length(), w2.length()); j++) {
char c1 = w1.charAt(j);
char c2 = w2.charAt(j);
if (c1 == c2) {
continue;
} else {
g.get(c1).add(c2);
inDegree.put(c2, inDegree.getOrDefault(c2, 0) + 1);
break;
}
}
}
StringBuilder sb = new StringBuilder();
Queue<Character> q = new ArrayDeque<>();
for (Character c: inDegree.keySet()) {
if (inDegree.get(c).equals(0)) {
q.add(c);
}
}
while (q.size() > 0) {
Character c = q.poll();
sb.append(c);
for (Character nei: g.get(c)) {
inDegree.put(nei, inDegree.get(nei) - 1);
if (inDegree.get(nei).equals(0)) {
q.add(nei);
}
}
}
if (sb.length() < inDegree.size()) return "";
return sb.toString();
}
}
|