Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 1013 Bytes

leetcode_451.md

File metadata and controls

37 lines (29 loc) · 1013 Bytes

LeetCode Problems

451. Sort Characters By Frequency

class Solution {
    fun frequencySort(s: String): String {
        var result = mutableListOf<Char>()
        // to sort map
        LinkedList(
            // create map
            mutableMapOf<Char, Int>().apply {
                // count each character's occurance
                s.forEach { c -> 
                    this[c] ?.let { this[c] = this[c]!! + 1 } ?: run { this[c] = 1 }
                }
            }.entries
        )
            .sortedByDescending { it.value }    // sort map.entries by count's decreasing order
            .forEach { (k, v) ->
                // add each character with repeat by count
                for (i in 1..v) result.add(k)
            }
        return result.joinToString("")
    }
}