Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lcof2/剑指 Offer II 114. 外星文字典/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,78 @@ func alienOrder(words []string) string {
}
```

#### Swift

```swift
class Solution {
func alienOrder(_ words: [String]) -> String {
var graph = Array(repeating: Set<Int>(), count: 26)
var indegree = Array(repeating: 0, count: 26)
var seen = Array(repeating: false, count: 26)
var letterCount = 0

for i in 0..<words.count - 1 {
for char in words[i] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
if !seen[index] {
seen[index] = true
letterCount += 1
}
}
let minLength = min(words[i].count, words[i + 1].count)
for j in 0..<minLength {
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]

if char1 != char2 {
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)

if !graph[c1].contains(c2) {
graph[c1].insert(c2)
indegree[c2] += 1
}
break
}

if j == minLength - 1 && words[i].count > words[i + 1].count {
return ""
}
}
}

for char in words[words.count - 1] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
if !seen[index] {
seen[index] = true
letterCount += 1
}
}

var queue = [Int]()
for i in 0..<26 {
if seen[i] && indegree[i] == 0 {
queue.append(i)
}
}

var order = ""
while !queue.isEmpty {
let u = queue.removeFirst()
order += String(UnicodeScalar(u + Int(Character("a").asciiValue!))!)
for v in graph[u] {
indegree[v] -= 1
if indegree[v] == 0 {
queue.append(v)
}
}
}

return order.count == letterCount ? order : ""
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
67 changes: 67 additions & 0 deletions lcof2/剑指 Offer II 114. 外星文字典/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Solution {
func alienOrder(_ words: [String]) -> String {
var graph = Array(repeating: Set<Int>(), count: 26)
var indegree = Array(repeating: 0, count: 26)
var seen = Array(repeating: false, count: 26)
var letterCount = 0

for i in 0..<words.count - 1 {
for char in words[i] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
if !seen[index] {
seen[index] = true
letterCount += 1
}
}
let minLength = min(words[i].count, words[i + 1].count)
for j in 0..<minLength {
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]

if char1 != char2 {
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)

if !graph[c1].contains(c2) {
graph[c1].insert(c2)
indegree[c2] += 1
}
break
}

if j == minLength - 1 && words[i].count > words[i + 1].count {
return ""
}
}
}

for char in words[words.count - 1] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
if !seen[index] {
seen[index] = true
letterCount += 1
}
}

var queue = [Int]()
for i in 0..<26 {
if seen[i] && indegree[i] == 0 {
queue.append(i)
}
}

var order = ""
while !queue.isEmpty {
let u = queue.removeFirst()
order += String(UnicodeScalar(u + Int(Character("a").asciiValue!))!)
for v in graph[u] {
indegree[v] -= 1
if indegree[v] == 0 {
queue.append(v)
}
}
}

return order.count == letterCount ? order : ""
}
}
Loading