查询字符串中的最长重复子串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zouheliang/article/details/80649584

   要查询字符串中的子串,并且子串中存在的,比他短的子串不再计数

package ***.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Utf8;


public class T {


	public static void main(String[] args)
	{
		String str="2018新款夏无袖雪纺女连衣裙连衣裙女装女装中长款休闲显瘦连衣裙修身裙子";
		HashMap<String,Integer> map=new HashMap<>();
		List<String> dupStr=new ArrayList<>();
		int e=str.length();//比较的
		for(int i=e-1;i>1;i--)
		{
			for(int s=0;s<i-1;s++)
			{
				String sourceStr=str.substring(s,i);
				String replaceAll = str.replaceAll(sourceStr, "");
				int length=replaceAll.length()+sourceStr.length();
				if(e==length)
				{
					//只存在一个
					continue;
				}else
				{
					//重复了,记录下来
					if(checkExist(dupStr,sourceStr))
					{
						map.put(sourceStr, (e-length)/sourceStr.length());
						dupStr.add(sourceStr);
					}
				}
			}
			
		}
		
	System.out.println(map+"   ");
	}

private static boolean checkExist( List<String> dupStr, String sourceStr )
{
	boolean needRecord=true;
	for ( String str : dupStr)
	{
		if(str.contains(sourceStr))
		{
			return false;
		}
	}
	return needRecord;
}
	
	
}

  比如再上述字符串中存在重复的字符串:2018新款夏无袖雪纺女连衣裙连衣裙女装女装中长款休闲显瘦连衣裙修身裙子

测试的结果:{连衣裙=2, 女装=1} ,女装重复一次,连衣裙重复两次

猜你喜欢

转载自blog.csdn.net/zouheliang/article/details/80649584