Last active
November 3, 2017 21:36
-
-
Save madson/4343a8622b926bf2704eb0711d901ebb to your computer and use it in GitHub Desktop.
Super Reduced String - https://www.hackerrank.com/challenges/reduced-string/problem
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 text = "baabc" | |
func removeDuplicatedCharacters(in text: String) -> String { | |
var other = text | |
var uniqueChars = [String]() | |
for char in other.characters { | |
let current = char.description | |
if !uniqueChars.contains(current) { | |
uniqueChars.append(current) | |
} | |
} | |
for char in uniqueChars { | |
let dup = "\(char)\(char)" | |
if other.contains(dup) { | |
other = other.replacingOccurrences(of: dup, with: "") | |
other = removeDuplicatedCharacters(in: other) | |
} | |
} | |
if other == "" { | |
other = "Empty String" | |
} | |
return other | |
} | |
print(removeDuplicatedCharacters(in: text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment