Created
February 23, 2017 17:54
-
-
Save AnthonyBY/1a81c8e2b98d381f06e074cbb7986841 to your computer and use it in GitHub Desktop.
Strings: Making Anagrams Swift 3.1 Cracking the Coding Interview
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 | |
var string1 = String(readLine()!)! | |
var string2 = String(readLine()!)! | |
var count = string1.characters.count + string2.characters.count | |
var tempString1 = string1 | |
var tempString2 = string2 | |
for character in string1.characters { | |
if tempString2.contains(String(character)){ | |
count = count - 2 | |
let stringRange1 = tempString1.range(of: String(character)) | |
let stringRange2 = tempString2.range(of: String(character)) | |
tempString1 = tempString1.replacingOccurrences(of: String(character), with: "", options: .literal, range: stringRange1) | |
tempString2 = tempString2.replacingOccurrences(of: String(character), with: "", options: .literal, range: stringRange2) | |
} | |
} | |
print(count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment