From 58d2e38c6d23cedd09c65b214c2564a36566d9c7 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 30 Oct 2024 08:40:15 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.114 --- .../README.md" | 72 +++++++++++++++++++ .../Solution.swift" | 67 +++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" index 5929b469c698e..cace5e2395655 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" @@ -349,6 +349,78 @@ func alienOrder(words []string) string { } ``` +#### Swift + +```swift +class Solution { + func alienOrder(_ words: [String]) -> String { + var graph = Array(repeating: Set(), count: 26) + var indegree = Array(repeating: 0, count: 26) + var seen = Array(repeating: false, count: 26) + var letterCount = 0 + + for i in 0.. 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 : "" + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.swift" new file mode 100644 index 0000000000000..8f6ed449fed7f --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.swift" @@ -0,0 +1,67 @@ +class Solution { + func alienOrder(_ words: [String]) -> String { + var graph = Array(repeating: Set(), count: 26) + var indegree = Array(repeating: 0, count: 26) + var seen = Array(repeating: false, count: 26) + var letterCount = 0 + + for i in 0.. 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 : "" + } +}