Skip to content

Instantly share code, notes, and snippets.

@madson
Created October 5, 2017 23:44
Show Gist options
  • Save madson/ff7ab84d41d6dbed3fa7b368cfcae1e7 to your computer and use it in GitHub Desktop.
Save madson/ff7ab84d41d6dbed3fa7b368cfcae1e7 to your computer and use it in GitHub Desktop.
import Foundation
extension String {
var sortedChars: String {
get {
var current = ""
self.sorted().forEach {
if $0 != " " {
current.append($0)
}
}
return current
}
}
}
var anagrams: [String] = ["pear", "amleth", "dormitory", "tinsel",
"dirty room", "hamlet", "listen", "silent"]
var used: [String] = [String]()
for word in anagrams.sorted() {
if used.contains(word) == false {
var array: [String] = [word]
used.append(word)
anagrams.forEach {
if used.contains($0) == false && word.sortedChars == $0.sortedChars {
used.append($0)
array.append($0)
}
}
print(array.sorted())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment