Last active
July 8, 2017 16:24
-
-
Save StevenMasini/a730423b2cbd83ecabd20f42e64acf2a to your computer and use it in GitHub Desktop.
My implementation of the Permutation algorithm in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Permutator { | |
class func permutation(_ str: String) -> Set<String> { | |
return permutation(str, prefix: "") | |
} | |
private class func permutation(_ str: String, prefix: String) -> Set<String> { | |
if str.characters.count == 0 { | |
return [prefix] | |
} | |
var set = Set<String>() | |
for i in str.characters.indices { | |
let left = str.substring(to: i) | |
let right = str.substring(from: str.index(after: i)) | |
let rem = left + right | |
set = set.union(permutation(rem, prefix: prefix + String(str[i]))) | |
} | |
return set | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment