이때 튜플은 빈도수로 많이 나온 값이 앞에 오도록 정렬하는 문제입니다
import Foundation
func solution(_ s:String) -> [Int] {
var answer: [Int:Int] = [:]
var list = s
while !list.isEmpty {
var tmp = ""
if list.removeLast() == "}" {
if list.last! == "}" {
list.removeLast()
}
while list.last != "{" {
tmp += String(list.removeLast())
}
let mappedValue = tmp.reversed().map { String($0) }.joined()
let splitedValue = mappedValue.split(separator: ",").map { Int($0)! }
splitedValue.forEach { num in
if let value = answer[num] {
answer[num] = value + 1
} else {
answer[num] = 1
}
}
list.removeLast()
}
list.removeLast()
}
return answer.sorted(by: { $0.value == $1.value ? $0.key < $1.key : $0.value > $1.value }).map { $0.key }
}