Skip to content

Commit 4813fac

Browse files
committed
Refactor the solution to the alien dictionary
1 parent 2985760 commit 4813fac

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

Graph/AlienDictionary.swift

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,65 @@
99

1010
class AlienDictionary {
1111
func alienOrder(_ words: [String]) -> String {
12-
var res = "", queueChars = [Character]()
13-
var (indegrees, charToChars) = initGraph(words)
12+
var (inDegrees, toChars) = buildGraph(words)
1413

15-
indegrees.keys.filter { indegrees[$0] == 0 }.forEach { queueChars.append($0) }
14+
var queue = inDegrees.keys.filter { inDegrees[$0] == 0 }
15+
var res = ""
1616

17-
while !queueChars.isEmpty {
18-
let char = queueChars.removeFirst()
19-
res.append(char)
17+
while !queue.isEmpty {
18+
let char = queue.removeFirst()
2019

21-
guard let toChars = charToChars[char] else {
22-
fatalError("Init Graph Error")
23-
}
20+
res.append(char)
2421

25-
for toChar in toChars {
26-
guard let indegree = indegrees[toChar] else {
27-
fatalError("Init Graph Error")
28-
}
22+
for nextChar in toChars[char]! {
23+
inDegrees[nextChar]! -= 1
2924

30-
indegrees[toChar] = indegree - 1
31-
if indegree == 1 {
32-
queueChars.append(toChar)
25+
if inDegrees[nextChar] == 0 {
26+
queue.append(nextChar)
3327
}
3428
}
3529
}
3630

37-
return res.count == indegrees.count ? res : ""
31+
return res.count == inDegrees.count ? res : ""
3832
}
3933

40-
private func initGraph(_ words: [String]) -> ([Character: Int], [Character: [Character]]) {
41-
var indegrees = [Character: Int](), charToChars = [Character: [Character]
34+
private func buildGraph(_ words: [String]) -> ([Character: Int], [Character: [Character]]) {
35+
// init inDegrees and toChars
36+
var inDegrees = [Character: Int](), toChars = [Character: [Character]]()
4237

43-
// init indegress and charToChars
4438
words.forEach { word in
4539
word.forEach { char in
46-
indegrees[char] = 0
47-
charToChars[char] = [Character]()
48-
}
40+
inDegrees[char] = 0
41+
toChars[char] = [Character]()
42+
}
4943
}
5044

51-
// refactor indegress and charToChars based on words
45+
// update based on orders
5246
for i in 0..<words.count - 1 {
53-
let currentWord = Array(words[i]), nextWord = Array(words[i + 1])
54-
55-
for j in 0..<min(currentWord.count, nextWord.count) {
56-
let currentChar = currentWord[j], nextChar = nextWord[j]
57-
58-
if nextChar == currentChar {
59-
47+
let left = Array(words[i]), right = Array(words[i + 1])
48+
49+
for j in 0..<min(left.count, right.count) {
50+
if left[j] == right[j] {
51+
52+
// invalid use case
6053
if j + 1 == right.count && right.count < left.count {
6154
return ([Character: Int](), [Character: [Character]]())
6255
}
63-
56+
6457
continue
6558
}
6659

67-
if let toChars = charToChars[currentChar], toChars.contains(nextChar) {
60+
if toChars[left[j]]!.contains(right[j]) {
6861
break
6962
}
70-
71-
indegrees[nextChar, default: 0] += 1
72-
charToChars[currentChar, default: [Character]()].append(nextChar)
63+
64+
inDegrees[right[j]]! += 1
65+
toChars[left[j]]!.append(right[j])
7366
break
67+
7468
}
75-
7669
}
77-
78-
return (indegrees, charToChars)
70+
71+
return (inDegrees, toChars)
7972
}
8073
}

0 commit comments

Comments
 (0)