【算法】寻找一个字符串中出现次数最多的字符以及出现的次数

    科技2023-10-28  104

    查找法

    public class FindTheMostAppearChar { public static void main(String[] args) { hashMapMethodToAchieve(); } /** * 用字符数组查找法实现 * 解题思路:先将字符串拆分成字符数组,然后转存到 HashMap 集合中, * 该集合的key为字符串中出现的字符,value为对应字符串出现的次数。 * 最后只需要在HashMap集合中找到Value值最大的key即可。 */ public static void hashMapMethodToAchieve() { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine().trim(); scanner.close(); // 将字符串转换成字符数组 char[] arr = string.toCharArray(); Map<Character, Integer> map = new HashMap<>(); // key为出现的字符,value 为该字符出现的次数,将字符数组转存在 HashMap 中 if (arr != null && arr.length > 0) { for (int i = 0; i < arr.length; i++) { if (map.get(arr[i]) != null) { // 若不为空,说明已经存在相同的字符,则 value 值在原来的基础上加1 map.put(arr[i],map.get(arr[i]) + 1); } else { map.put(arr[i], 1); } } } // 查找出出现次数最多的字符以及出现的次数也有多种写法 FindTheMostCharByMap(map); // 查找写法一:用 Iterator 遍历 Map 来查找 // FindTheMostCharByMapEntry(map); // 查找写法二:用 Map.Entry 提高效率 // FindTheMostCharByForLoop(map, arr); // 查找写法三:直接用 for 循环来遍历查找 } // 查找写法一:用 Iterator 遍历 Map 来查找 public statice void FindTheMostCharByMap(Map<Character, Integer> map) { Set<Character> keys = map.keySet(); // 获取所有的key Iterator iterator = keys.iterator(); // 实例化 Iterator Character maxKey = (Character) iterator.next(); //定义第一个为最大的value和对应的key int maxValue = map.get(maxKey); while (iterator.hasNext()) { Character temp = (Character) iterator.next(); if (maxValue < map.get(temp)) { maxKey = temp; maxValue = map.get(temp); } } System.out.println("出现次数最多的字符是:" + maxKey + ", 出现的次数:" + maxValue); } // 查找写法二:用 Map.Entry 提高效率 public static void FindTheMostCharByMapEntry(Map<Character, Integer> map) { Iterator iterator = map.entrySet().iterator(); Map.Entry entry = (Map.Entry) iterator.next(); char maxKey = (char) entry.getKey(); // 获取key int maxValue = (int) entry.getValue(); // 获取value while (iterator.hasNext()) { entry = (Map.Entry) iterator.next(); char tempKey = (char) entry.getKey(); int t

    删除法

    /** * 用删除法实现 (挺巧妙的) * 解题思路:每次取出字符串的第一个字符,将字符串中与第一个字符相同的字符全部删掉, * 然后通过计算删除前后字符串的长度来确定该字符在字符串出现的次数,最终比较出出现次数最多的字符 */ class Solution { Map<String,Integer> deleteMethodToAchieve(){ System.out.println("请输入一段字符串"); Scanner s1=new Scanner(System.in); String s2 =s1.nextLine().trim(); //读取输入的一行,并去掉首尾空格 int max=0; String strmax=""; while (s2.length()>0){ int prelength=s2.length(); String firstchar=s2.substring(0,1); s2=s2.replaceAll(firstchar,""); int curlength=s2.length(); if(prelength-curlength>max){ //注意是prelength -curlength max=prelength-curlength; strmax=firstchar; } } Map <String,Integer> res= new HashMap<>(); res.put(strmax,max); return res; } }

    排序法(2种实现)

    //寻找一个字符串中出现次数最多的字符以及出现的次数,排序法 /** * 用排序法实现 * 解题思路:先将字符串转换成字符数组,然后对字符数组进行排序, * 统计每个字符重复出现的次数,最后比较得出出现次数最多的字符以及出现次数 */ class Solution { Map<Character,Integer> sortMethodToAchieve() { Scanner s1=new Scanner(System.in); String str_input=s1.nextLine(); char[] s2=str_input.toCharArray(); Arrays.sort(s2); char temp_char=s2[0]; int temp_cishu=0; char max_char=s2[0]; int max_cishu=0; for(char i : s2){ if (temp_char==i){ temp_cishu+=1; } else { temp_char=i; temp_cishu=1; } if(temp_cishu>max_cishu){ max_cishu=temp_cishu; max_char=temp_char; } } Map <Character,Integer> res= new HashMap<>(); res.put(max_char,max_cishu); return res; } #方法2 public static void sortMethodToAchieve2() { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine().trim(); scanner.close(); char[] arr = string.toCharArray(); Arrays.sort(arr); // 对数组进行排序 char maxValue = 'a'; // 记录出现次数最多的元素 int maxCount = 1; // 记录出现次数 int count = 1; for (int i = 0; i < arr.length - 1; i++) { if (arr[i] == arr[i+1]) { count++; } if (arr[i] != arr[i+1]) { count = 1; } if (count > maxCount) { maxCount = count; maxValue = arr[i]; } } System.out.println("出现次数最多的字符是:" + maxValue + ", 出现的次数:" + maxCount); } }

    参考链接 https://segmentfault.com/a/1190000019536395?utm_source=tag-newest

    Processed: 0.008, SQL: 8