From d499e622d593345b1e746aaab0dcbcf976f13cd1 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 2 Sep 2024 11:57:40 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.086 (#3471) --- .../README.md" | 43 +++++++++++++++++++ .../Solution.swift" | 38 ++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 29b5e9db2fcee..7fffa67082866 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -285,6 +285,49 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + private var n: Int = 0 + private var s: String = "" + private var f: [[Bool]] = [] + private var t: [String] = [] + private var ans: [[String]] = [] + + func partition(_ s: String) -> [[String]] { + n = s.count + self.s = s + f = Array(repeating: Array(repeating: true, count: n), count: n) + + let chars = Array(s) + + for i in stride(from: n - 1, through: 0, by: -1) { + for j in i + 1 ..< n { + f[i][j] = chars[i] == chars[j] && f[i + 1][j - 1] + } + } + + dfs(0) + return ans + } + + private func dfs(_ i: Int) { + if i == n { + ans.append(t) + return + } + for j in i ..< n { + if f[i][j] { + t.append(String(s[s.index(s.startIndex, offsetBy: i)...s.index(s.startIndex, offsetBy: j)])) + dfs(j + 1) + t.removeLast() + } + } + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.swift" new file mode 100644 index 0000000000000..23a5d4200ef8d --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.swift" @@ -0,0 +1,38 @@ +class Solution { + private var n: Int = 0 + private var s: String = "" + private var f: [[Bool]] = [] + private var t: [String] = [] + private var ans: [[String]] = [] + + func partition(_ s: String) -> [[String]] { + n = s.count + self.s = s + f = Array(repeating: Array(repeating: true, count: n), count: n) + + let chars = Array(s) + + for i in stride(from: n - 1, through: 0, by: -1) { + for j in i + 1 ..< n { + f[i][j] = chars[i] == chars[j] && f[i + 1][j - 1] + } + } + + dfs(0) + return ans + } + + private func dfs(_ i: Int) { + if i == n { + ans.append(t) + return + } + for j in i ..< n { + if f[i][j] { + t.append(String(s[s.index(s.startIndex, offsetBy: i)...s.index(s.startIndex, offsetBy: j)])) + dfs(j + 1) + t.removeLast() + } + } + } +} \ No newline at end of file