字符串中出现次数最多的字符算法

12 篇文章 0 订阅
6 篇文章 0 订阅

 求一个字符串中出现次数最多的字符算法总结如下

 

/*
*Class GetMaxChar.java
*Create Date: 2009-11-25
*Author:a276202460
*/
package com.rich.notation;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class GetMaxCountChar {
	public static void main(String[] s){
		String str = "jlfjdlsuoeilkjalfdufielgjlafodiglldjfdjflweifjlgjsdlgjagjlasjgl";
		System.out.println(printmaxcountcharMap(str));
		System.out.println(printmaxcountcharASII(str));
		System.out.println(printmaxcountcharArray(str));
		long starttime = System.currentTimeMillis();
		for(int i = 0 ;i < 20000;i++)
		printmaxcountcharASII(str);
		System.out.println(System.currentTimeMillis() - starttime);
	    
		starttime = System.currentTimeMillis();
		for(int i = 0 ;i < 20000;i++)
		printmaxcountcharArray(str);
		System.out.println(System.currentTimeMillis() - starttime);
		
		starttime = System.currentTimeMillis();
		for(int i = 0 ;i < 20000;i++)
		printmaxcountcharMap(str);
		System.out.println(System.currentTimeMillis() - starttime);
	}
	/*
	 * CSDN shine333 提供,堪称经典(至少我没想到根据char去找对应的count值,所以我个人认为经典,如果有好的算法还请贴出来大家共享之)
	 */
   public static String printmaxcountcharASII(String str){
	   int[] count = new int[128];
	    for (int i = str.length(); i-- > 0; count[str.charAt(i)]++);
	    int maxIndex = 0;
	    for (int i = 1; i < count.length; i++) {
	      if (count[i] > count[maxIndex]) {
	        maxIndex = i;
	      }
	    }
	   return (char)maxIndex+":"+count[maxIndex];

   }
    /*
     * CSDN a276202460 菜鸟提供 
     * 缺点排序浪费时间,和串长度成正比
     */
   public static String printmaxcountcharArray(String str){
	   char[] chars = str.toCharArray();
       Arrays.sort(chars);
       int max = 0;
       int count = 0;
       int maxchar = 0;
       int oldchar = 0 ;
       for(int i = 0 ;i <= chars.length;i++){
          
           if(i == 0 || i == chars.length || oldchar != chars[i]  ){
                max = max > count?max:count;
                maxchar = max > count?maxchar:oldchar;
                if(i == chars.length){
                    break;
                }
                oldchar = chars[i];
                count = 0;
             }
           count++;
           
       }
       return (char)maxchar+":"+max;

   }
   
   /*
    * CSDN 另一网友提出用map 以下我补充完代码
    * 缺点map判断和修改map结构浪费时间,另外还要迭代
    */
   public static String printmaxcountcharMap(String str){
	   Map<Character,Integer> charmap = new HashMap<Character,Integer>();
	   char curchar = 0;
	   for(int i = 0 ;i < str.length();i++){
		   curchar = str.charAt(i);
		   if(charmap.containsKey(curchar)){
			   charmap.put(curchar, charmap.get(curchar) + 1);
		   }else{
			   charmap.put(curchar, 1);
		   }
	   }
	   char maxchar = 0;
	   int maxcount = 0;
	   
	   Iterator it = charmap.keySet().iterator();
	   while(it.hasNext()){
		   curchar = (Character)it.next();
		   if(charmap.get(curchar) > maxcount){
			   maxchar = curchar;
			   maxcount = charmap.get(curchar);
		   }
	   }
	   return maxchar+":"+charmap.get(maxchar);
   }
}

运行结果:

l:13
l:13
l:13
62
94
281

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值