---
title: "一个面试题寻找字符串连续重复次数最多的字符和个数"
author: "nickdine"
date: "2010-08-30T15:01:50Z"
tags: ["Java", "面试"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-08-30T15:01:50Z"
id: 750806
url: "https://nickdine.iteye.com/blog/750806"
---

```java
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();
	}
}

```