Skip to content

Instantly share code, notes, and snippets.

@alex-ross
Last active December 5, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-ross/24b95a689dac88a976dbb8ba8b128470 to your computer and use it in GitHub Desktop.
Save alex-ross/24b95a689dac88a976dbb8ba8b128470 to your computer and use it in GitHub Desktop.
import Foundation
let argument = "REPLACE WITH YOUR INPUT"
public extension Character {
public func swappedcased() -> Character {
let lower = String(self).lowercased().first!
if lower == self {
return String(self).uppercased().first!
} else {
return lower
}
}
}
func react(on string: String) -> String {
return string.reduce("") { (result, character) -> String in
if let lastInsertedCharacter = result.last, lastInsertedCharacter == character.swappedcased() {
return String(result.dropLast())
}
return result + String(character)
}
}
func product(from string: String) -> Int {
return react(on: string).count
}
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".map { $0 }
var products = [Int]()
for character in alphabet {
let string = argument.replacingOccurrences(of: String(character), with: "")
.replacingOccurrences(of: String(character).lowercased(), with: "")
products.append(product(from: string))
}
print("Step 1: \(product(from: argument))")
if let step2 = products.min() {
print("Step 2: \(step2)")
}
@alex-ross
Copy link
Author

Remove dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment