Skip to content

Commit 515b1fc

Browse files
authored
feat: add swift implementation to lcof2 problem: No.114 (#3692)
1 parent c80128c commit 515b1fc

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

lcof2/剑指 Offer II 114. 外星文字典/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,78 @@ func alienOrder(words []string) string {
349349
}
350350
```
351351

352+
#### Swift
353+
354+
```swift
355+
class Solution {
356+
func alienOrder(_ words: [String]) -> String {
357+
var graph = Array(repeating: Set<Int>(), count: 26)
358+
var indegree = Array(repeating: 0, count: 26)
359+
var seen = Array(repeating: false, count: 26)
360+
var letterCount = 0
361+
362+
for i in 0..<words.count - 1 {
363+
for char in words[i] {
364+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
365+
if !seen[index] {
366+
seen[index] = true
367+
letterCount += 1
368+
}
369+
}
370+
let minLength = min(words[i].count, words[i + 1].count)
371+
for j in 0..<minLength {
372+
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
373+
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]
374+
375+
if char1 != char2 {
376+
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
377+
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)
378+
379+
if !graph[c1].contains(c2) {
380+
graph[c1].insert(c2)
381+
indegree[c2] += 1
382+
}
383+
break
384+
}
385+
386+
if j == minLength - 1 && words[i].count > words[i + 1].count {
387+
return ""
388+
}
389+
}
390+
}
391+
392+
for char in words[words.count - 1] {
393+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
394+
if !seen[index] {
395+
seen[index] = true
396+
letterCount += 1
397+
}
398+
}
399+
400+
var queue = [Int]()
401+
for i in 0..<26 {
402+
if seen[i] && indegree[i] == 0 {
403+
queue.append(i)
404+
}
405+
}
406+
407+
var order = ""
408+
while !queue.isEmpty {
409+
let u = queue.removeFirst()
410+
order += String(UnicodeScalar(u + Int(Character("a").asciiValue!))!)
411+
for v in graph[u] {
412+
indegree[v] -= 1
413+
if indegree[v] == 0 {
414+
queue.append(v)
415+
}
416+
}
417+
}
418+
419+
return order.count == letterCount ? order : ""
420+
}
421+
}
422+
```
423+
352424
<!-- tabs:end -->
353425

354426
<!-- solution:end -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Solution {
2+
func alienOrder(_ words: [String]) -> String {
3+
var graph = Array(repeating: Set<Int>(), count: 26)
4+
var indegree = Array(repeating: 0, count: 26)
5+
var seen = Array(repeating: false, count: 26)
6+
var letterCount = 0
7+
8+
for i in 0..<words.count - 1 {
9+
for char in words[i] {
10+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
11+
if !seen[index] {
12+
seen[index] = true
13+
letterCount += 1
14+
}
15+
}
16+
let minLength = min(words[i].count, words[i + 1].count)
17+
for j in 0..<minLength {
18+
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
19+
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]
20+
21+
if char1 != char2 {
22+
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
23+
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)
24+
25+
if !graph[c1].contains(c2) {
26+
graph[c1].insert(c2)
27+
indegree[c2] += 1
28+
}
29+
break
30+
}
31+
32+
if j == minLength - 1 && words[i].count > words[i + 1].count {
33+
return ""
34+
}
35+
}
36+
}
37+
38+
for char in words[words.count - 1] {
39+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
40+
if !seen[index] {
41+
seen[index] = true
42+
letterCount += 1
43+
}
44+
}
45+
46+
var queue = [Int]()
47+
for i in 0..<26 {
48+
if seen[i] && indegree[i] == 0 {
49+
queue.append(i)
50+
}
51+
}
52+
53+
var order = ""
54+
while !queue.isEmpty {
55+
let u = queue.removeFirst()
56+
order += String(UnicodeScalar(u + Int(Character("a").asciiValue!))!)
57+
for v in graph[u] {
58+
indegree[v] -= 1
59+
if indegree[v] == 0 {
60+
queue.append(v)
61+
}
62+
}
63+
}
64+
65+
return order.count == letterCount ? order : ""
66+
}
67+
}

0 commit comments

Comments
 (0)