题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

package main

import (
	"fmt"
	"sort"
)

func main() {
    var str string
    fmt.Scan(&str)
    mp := make(map[rune]int)
    res := make([]rune, 0, 40)
    for _, item := range str {
        if _, ok := mp[item]; !ok {
            res = append(res, item)
        }
        mp[item]++
    }
    sort.Slice(res, func(i, j int) bool{
        if mp[res[i]] == mp[res[j]] {
            return res[i] < res[j]
        }
        return mp[res[i]] > mp[res[j]]
    })
    fmt.Println(string(res))
}

全部评论

相关推荐

今年读完研的我无房无车无对象,月入还没有过万&nbsp;看到他在朋友圈晒房产证,感叹自己白读了这么多年书
小浪_Coding:学历不代表就能赚多少钱, 自己硕士学历怎么说也是一方面好事, 工作只是为了谋生, 赚钱跟学历不挂钩, 看自己走什么样的路,做什么选择
点赞 评论 收藏
分享
04-27 08:59
常州大学 Java
牛客139242382号:《两门以上汇编语言》
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务