Microsoft OA
2024-08-26 10:22:20
Given a string S consisting of N lowercase letters, return the minimum number of letters that must be deleted to obtain a word in which every letter occurs a unique number of times
没有想到要再存一个HashMap, appearance to character mapping
package UniqueCharacter; import java.util.HashMap;
import java.util.Map; public class Solution {
public static int charCountToDelete(String s) {
HashMap<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
} int res = 0;
HashMap<Integer, Character> intToCharMap = new HashMap<>();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
int value = entry.getValue();
while (intToCharMap.containsKey(value)) {
res ++; // need to delete
value --;
}
intToCharMap.put(value, entry.getKey());
}
return res;
} public static void main(String[] args) {
int res = charCountToDelete("aaaabbbbcccdde");
System.out.printf("result is %d\n", res);
int res2 = charCountToDelete("aaaabbbb");
System.out.printf("result is %d\n", res2);
int res3 = charCountToDelete("aaaabbbccd");
System.out.printf("result is %d\n", res3);
}
}
最新文章
- Mac-OSX的Python3.5虚拟环境下安装Opencv
- Smart3D系列教程3之 《论照片三维重建中Smart3D几个工作模块的功能意义》
- MySQL多实例,主从同步
- 弃用的同步get和post请求
- 修改ECSHOP系统红包序列号规律
- opencl gauss filter优化(二)
- FastDFS安装配置
- 基于Lucene的文件检索Demo
- 关于UIScrollView属性和方法的总结
- JavaWeb学习笔记--2.3内置对象
- css:hover选择器
- 一天一个类 --- StringTokenizer
- Swift - 使用NSURLSession同步获取数据(通过添加信号量)
- Dynamics CRM 删除字段时检测到有组件类型为查看的依赖组件而无法删除问题
- django项目上线环境部署
- Spring在web开发中的应用
- MXNET:深度学习计算-模型参数
- 【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
- Win10怎么设置打开文件的默认程序
- Jmeter(十四)Logic Controller 之 If Controller
热门文章
- 那些可作为GC Roots的对象
- 爬虫(二)-创建项目&;应用
- [转]Linux虚拟网络设备之tun/tap
- Handling skewed data---Error metrics for skewed(偏斜的) classes(precision&;recall)
- Tensorflow细节-P170-图像数据预处理
- django 第三天 视图
- RestTemplate 使用中的几个问题
- c函数指针和指针函数如何使用何定义;如何调用使用
- EasyUI日期时间框DateTimeBox
- mysql group by order by havaing where 顺序