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);
}
}

最新文章

  1. Mac-OSX的Python3.5虚拟环境下安装Opencv
  2. Smart3D系列教程3之 《论照片三维重建中Smart3D几个工作模块的功能意义》
  3. MySQL多实例,主从同步
  4. 弃用的同步get和post请求
  5. 修改ECSHOP系统红包序列号规律
  6. opencl gauss filter优化(二)
  7. FastDFS安装配置
  8. 基于Lucene的文件检索Demo
  9. 关于UIScrollView属性和方法的总结
  10. JavaWeb学习笔记--2.3内置对象
  11. css:hover选择器
  12. 一天一个类 --- StringTokenizer
  13. Swift - 使用NSURLSession同步获取数据(通过添加信号量)
  14. Dynamics CRM 删除字段时检测到有组件类型为查看的依赖组件而无法删除问题
  15. django项目上线环境部署
  16. Spring在web开发中的应用
  17. MXNET:深度学习计算-模型参数
  18. 【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
  19. Win10怎么设置打开文件的默认程序
  20. Jmeter(十四)Logic Controller 之 If Controller

热门文章

  1. 那些可作为GC Roots的对象
  2. 爬虫(二)-创建项目&amp;应用
  3. [转]Linux虚拟网络设备之tun/tap
  4. Handling skewed data---Error metrics for skewed(偏斜的) classes(precision&amp;recall)
  5. Tensorflow细节-P170-图像数据预处理
  6. django 第三天 视图
  7. RestTemplate 使用中的几个问题
  8. c函数指针和指针函数如何使用何定义;如何调用使用
  9. EasyUI日期时间框DateTimeBox
  10. mysql group by order by havaing where 顺序