Ruby 99ms beats 100%


  • 0
    M
    def frequency_sort(s)
        arr = Array.new(256){|i| [i, 0]}
        s.each_byte{|b| arr[b][1] += 1}
        arr.sort_by!{|x| x[1]}.reverse!
        res = ""
        arr.each{|x|
            if (x[1] > 0)
                res <<= x[0].chr * x[1]
            end
        }
        res
    end

Log in to reply
 

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