forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0269-alien-dictionary.swift
57 lines (48 loc) · 1.38 KB
/
0269-alien-dictionary.swift
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
55
56
57
/**
* Question Link: https://leetcode.com/problems/alien-dictionary/
*/
class Solution {
func alienOrder(_ words: [String]) -> String {
var adj = [Character: Set<Character>]()
for word in words {
for char in word {
adj[char] = []
}
}
for i in 0..<words.count - 1 {
let w1 = Array(words[i]), w2 = Array(words[i + 1])
let minLen = min(w1.count, w2.count)
if w1.count > w2.count && w1.prefix(minLen) == w2.prefix(minLen) {
return ""
}
for j in 0..<minLen {
if w1[j] != w2[j] {
adj[w1[j], default: Set()].insert(w2[j])
break
}
}
}
var visited = [Character: Bool]()
var res = ""
func dfs(_ char: Character) -> Bool {
if visited[char] != nil {
return visited[char]!
}
visited[char] = true
for neighChar in adj[char]! {
if dfs(neighChar) {
return true
}
}
visited[char] = false
res.append(char)
return false
}
for char in adj.keys {
if dfs(char) {
return ""
}
}
return String(res.reversed())
}
}