`
nickdine
  • 浏览: 44240 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

一个面试题寻找字符串连续重复次数最多的字符和个数

阅读更多
package i.tommy.test.Test;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class matchers
{

	static String words = "ggggiuiiipppppaaaaahhhhddddd";

	public static void main(String[] args)
	{
		System.out.println(getLongs());
	}

	/*
	 * 返回连续单个字符出现最多次数的字符和个数
	 */
	public static String getLongs()
	{
		Pattern pattern = Pattern.compile("([a-zA-Z])\\1+");
		Matcher matcher = pattern.matcher(words);
		String word = "";
		while (matcher.find())
		{
			String index = matcher.group();
			if (word.length() < index.length())
			{
				word = index;
			}
		}
		
		return word.substring(0, 1) + "," + word.length();
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics