So this question is a follow up to this one: swift string permutations allowing the same strings
There, I asked about all possible mutations that I can make with a defined set of strings. What I am trying to do next is to filter out all results that have the same combination, but in a different order.
So if the input is: ["AB", "AC", "CA", "CB"]
, the output should be ["AB", "AC", "CB"]
, since "AC" and "CA" have the same building blocks.
So my thought was to first sort each string alphabetically, and maybe then create a Set
.
I am already stuck on the first part :(
let array = ["AB", "AC", "DC", "CA", "CB"]
print(type(of: array))
print(array)
let sortedArray = array.map{ $0.sorted() }
print(type(of: sortedArray))
print(sortedArray)
The output is:
Array<String>
["AB", "AC", "DC", "CA", "CB"]
Array<Array<Character>>
[["A", "B"], ["A", "C"], ["C", "D"], ["A", "C"], ["B", "C"]]
While I expected for the sortedArray:
["AB", "AC", "CD", "AC", "BC"]
Then I thought to join the individual strings back together:
print(array.map{ $0.joined() } )
Resulting in ambiguous reference to member 'joined()'
But how to fix this I don't understand.
I also saw this one: swift sort characters in a string where the following code is used:
var nonSortedString = "5121"
var sortedString = String(Array(nonSortedString.characters).sort())
But I don't see how to apply that using map
and friends (after converting to Swift 4)
Any help appreciated.