/*
Given a string, return a new string of equal length with the same
characters rearranged in decreasing order of number of occurrences. In
the case of two or more distinct characters occurring the same number
of times, order them by increasing character code.
This is a basic test of programming language familiarity with
little reliance on runtime library. It also requires basic algorithm construction, but nothing fancy.
Hint:
Example: given "This is a sample string",
return "ssssiiiaaTeghlmnprrt"
*/
这是一个字符串出现频率排序的题:
✅ 要求
- 给定字符串,将所有字符按 出现次数降序 排列。
- 如果多个字符出现次数相同,则按 字符 ASCII 值升序 排序(例如空格、大小写字母等)。
- 输出与原长度相同的新字符串。
✅ 解题思路(简明)
- 使用 hashmap 统计每个字符的频率
- 将字符按
(freq desc, char asc)排序 - 按排序结果重复字符并拼接输出
难点:
- 不能过分依赖库函数
- 需要自己设计排序逻辑
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 OpenAI、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完