zoukankan      html  css  js  c++  java
  • 判断字符串中出现次数最多的字符和出现次数

    package my;
    
    import java.util.HashMap;
    
    public class FindStringMaxSolution {
        int findStrMax(String str){
            if(str.length() == 0 || str == null){
                return -1;
            }
            int max =0;
            HashMap<Character , Integer> map = new HashMap<>();
            for(int i= 0 ; i < str.length() ; i++){
                char c = str.charAt(i);
                if(map.containsKey(c)){
                    map.put(c ,map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }

    for(int j=0 ;j <str.length(); j++){ max = Math.max(max ,map.get(str.charAt(j))); } return max; } public static void main(String[] args){ String str ="3w3wrr3rk3jr"; int n = new FindStringMaxSolution().findStrMax(str); System.out.println(n); } }
    package my;
    
    import java.util.HashMap;
    
    public class FindStringMaxSolution {
        int findStrMax(String str){
            if(str.length() == 0 || str == null){
                return -1;
            }
            int max =0;
            char charmax = 0;
            HashMap<Character , Integer> map = new HashMap<>();
            for(int i= 0 ; i < str.length() ; i++){
                char c = str.charAt(i);
                if(map.containsKey(c)){
                    map.put(c ,map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }
            for(char key : map.keySet()){
                if(map.get(key) > max){
                    max = map.get(key);
                     charmax = key;
                }
            }
            System.out.println(charmax);
            return max;
        }
        public static void main(String[] args){
            String str ="3w3wrr3rk3jr";
            int n = new FindStringMaxSolution().findStrMax(str);
            System.out.println(n);
        }
    }
  • 相关阅读:
    Node.js运行Vue项目
    DotNetCore知识栈
    Building gRPC Client iOS Swift Note Taking App
    React Native
    Node.js 教程
    SQL 在线教程&在线练习平台
    RxSwift + Moya + ObjectMapper
    浅谈常用的几种web攻击方式
    让MyEclipse支持mac的Retina屏解决字体模糊的问题
    Java设计模式中的单例模式
  • 原文地址:https://www.cnblogs.com/goodtest2018/p/13664423.html
Copyright © 2011-2022 走看看