Microsoft OA
发布时间:2020-12-14 05:55:44 所属栏目:Windows 来源:网络整理
导读: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
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 1 package UniqueCharacter; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class Solution { 7 public static int charCountToDelete(String s) { 8 HashMap<Character,Integer> map = new HashMap<>(); 9 for (char c : s.toCharArray()) { 10 map.put(c,map.getOrDefault(c,0) + 1); 11 } 12 13 int res = 0; 14 HashMap<Integer,Character> intToCharMap = new HashMap<>(); 15 for (Map.Entry<Character,Integer> entry : map.entrySet()) { 16 int value = entry.getValue(); 17 while (intToCharMap.containsKey(value)) { 18 res ++; // need to delete 19 value --; 20 } 21 intToCharMap.put(value,entry.getKey()); 22 } 23 return res; 24 } 25 26 public static void main(String[] args) { 27 int res = charCountToDelete("aaaabbbbcccdde"); 28 System.out.printf("result is %dn",res); 29 int res2 = charCountToDelete("aaaabbbb"); 30 System.out.printf("result is %dn",res2); 31 int res3 = charCountToDelete("aaaabbbccd"); 32 System.out.printf("result is %dn",res3); 33 } 34 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows – Application.Current在ElementHost为null
- windows – 如何将Module :: Build配置为NOT以只读方式安装
- 环境搭建文档——Windows下的Python3环境搭建
- windows – 从剪切区域中排除圆角矩形?
- windows-server-2008-r2 – Windows防火墙在重启后几乎阻止
- 如何查看Windows机器上是否正在使用端口?
- windows-xp – 如何在XP中创建Windows服务
- windows-xp – 如何让localhost在parallels桌面上运行xp
- Windows命令相当于egrep
- windows-server-2012-r2 – 无法传输控制器操作主机角色或降
推荐文章
站长推荐
- windows-7 – 如何在批处理文件中启动具有管理员
- Windows操作系统中是否存在零拷贝?
- win10家庭版升级专业版的一些经验方法(超简单,
- windows-phone-8 – Windows Phone 8页面生命周期
- windows7下composer安装不了或composer命令无效的
- active-directory – 从AD中删除旧DC,删除“子树
- osx – 当使用Windows服务器和Mac代理程序使用服
- 是否可以在Windows中执行Objective-C程序?
- 如何使用Windows C#应用程序的系统字体设置
- windows-8 – Windows 8 Winrt应用程序进入后台或
热点阅读