-
-
Save alex-ross/24b95a689dac88a976dbb8ba8b128470 to your computer and use it in GitHub Desktop.
This file contains 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
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)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove dependencies