My Swift Solution Easy to Understand


  • 0
    Y
    func frequencySort(_ s: String) -> String {
        var ans = ""
        var items:[Character:Int] = [:]
        for i in s.characters{
            items[i] = items[i] == nil ? 1 : items[i]!+1
        }
        let itemResult = items.sorted { (first: (key: Character, value: Int), second: (key: Character, value: Int)) -> Bool in
            return first.value > second.value
        } 
        for bas in itemResult{
            for _ in 0..<bas.value{
                ans.append(bas.key)
            }
        }
        return ans
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.