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:
Given the following words in dictionary,
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
The correct order is: "wertf".
Example 2:
Given the following words in dictionary,
[
"z",
"x"
]
The correct order is: "zx".
Example 3:
Given the following words in dictionary,
[
"z",
"x",
"z"
]
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.
class Solution {
func alienOrder(_ words: [String]) -> String {
if words.count == 0 {
return ""
}
var graph: [Character: Set<Character>] = [:]
var indegrees: [Character: Int] = [:]
for word in words {
for c in word {
indegrees[c] = 0
}
}
for i in stride(from: 1, to: words.count, by: 1) {
let prev = words[i - 1]
let current = words[i]
let len = min(prev.count, current.count)
for j in 0..<len {
let c1 = prev[prev.index(prev.startIndex, offsetBy: j)]
let c2 = current[current.index(current.startIndex, offsetBy: j)]
if c1 != c2 {
var set = graph[c1] ?? Set<Character>()
if !set.contains(c2) {
set.insert(c2)
graph[c1] = set
indegrees[c2] = (indegrees[c2] ?? 0) + 1
}
break
}
}
}
var queue: [Character] = indegrees.filter { $1 == 0 }.map { $0.0 }
var result = ""
while queue.count > 0 {
let c = queue.removeFirst()
result.append(c)
if let set = graph[c] {
for neighbor in set {
if let indegree = indegrees[neighbor] {
indegrees[neighbor] = indegree - 1
if indegrees[neighbor] == 0 {
queue.append(neighbor)
}
}
}
}
}
if result.count != indegrees.count {
return ""
}
return result
}
}
public class Solution {
public String alienOrder(String[] words) {
if (words == null || words.length == 0) {
return "";
}
Map<Character, Set<Character>> graph = new HashMap<>();
Map<Character, Integer> indegree = new HashMap<>();
String result = "";
for (String word : words) {
for (char c : word.toCharArray()) {
indegree.put(c, 0);
}
}
for (int i = 0; i < words.length - 1; i++) {
String currentWord = words[i];
String nextWord = words[i + 1];
int len = Math.min(currentWord.length(), nextWord.length());
for (int j = 0; j < len; j++) {
char c1 = currentWord.charAt(j);
char c2 = nextWord.charAt(j);
if (c1 != c2) {
Set<Character> set;
if (graph.containsKey(c1)) {
set = graph.get(c1);
} else {
set = new HashSet<Character>();
}
if (!set.contains(c2)) {
set.add(c2);
graph.put(c1, set);
indegree.put(c2, indegree.get(c2) + 1);
}
break;
}
}
}
System.out.print(indegree);
Queue<Character> queue = new LinkedList<>();
for (char c : indegree.keySet()) {
if (indegree.get(c) == 0) {
queue.offer(c);
}
}
System.out.print(queue);
while (!queue.isEmpty()) {
char c = queue.poll();
result += c;
if (graph.containsKey(c)) {
for (char neighbor : graph.get(c)) {
indegree.put(neighbor, indegree.get(neighbor) - 1);
if (indegree.get(neighbor) == 0) {
queue.offer(neighbor);
}
}
}
}
if (result.length() != indegree.size()) {
return "";
}
return result;
}
}
Hope this helps,
Michael