码迷,mamicode.com
首页 > 其他好文 > 详细

计算"aabbc"中最多的相同字母数

时间:2018-09-24 23:24:03      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:imp   static   class   hashmap   str   param   contain   rac   out   

package Test;

import java.util.HashMap;
import java.util.Map;
public class test3 {
		/**
		 * 计算"aabbc"中最多的相同字母数
		 * @param args
		 */
	public static void main(String[] args) {
		String str="aabbbcc";
		char arr[]=str.toCharArray();
		Map<Character,Integer> map=new HashMap<Character,Integer>();
		for (char c : arr) {
			if(map.containsKey(c)){
				map.put(c, (Integer)map.get(c)+1);
			}else{
				map.put(c, 1);
			}
		}
		int max=0;
		for (Map.Entry<Character, Integer> entry : map.entrySet()) {
			if(max<entry.getValue()){
				max=entry.getValue();
			}
		}
		System.out.println("最多相同字母数"+max);
	}
}

  

计算"aabbc"中最多的相同字母数

标签:imp   static   class   hashmap   str   param   contain   rac   out   

原文地址:https://www.cnblogs.com/v-lcc/p/9697031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!