From 6cde109466d16f475b5f2a1d752bec404a7dacdb Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 27 May 2024 08:58:07 +0100 Subject: [PATCH] Swift implementation for LCOF 38 --- .../README.md" | 37 +++++++++++++++++++ .../Solution.swift" | 32 ++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" index 99dca7b2cf85d..affa90e9e4a66 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" @@ -283,6 +283,43 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + private var ans: [String] = [] + private var cs: [Character] = [] + + func permutation(_ s: String) -> [String] { + cs = Array(s) + dfs(0) + return ans + } + + private func dfs(_ i: Int) { + if i == cs.count - 1 { + ans.append(String(cs)) + return + } + var vis: Set = [] + for j in i.. diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.swift" new file mode 100644 index 0000000000000..bbe8b0026efb8 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.swift" @@ -0,0 +1,32 @@ +class Solution { + private var ans: [String] = [] + private var cs: [Character] = [] + + func permutation(_ s: String) -> [String] { + cs = Array(s) + dfs(0) + return ans + } + + private func dfs(_ i: Int) { + if i == cs.count - 1 { + ans.append(String(cs)) + return + } + var vis: Set = [] + for j in i..