代码语言:txt复制
/**
* 获取两个字符串的最长重复子串
* @param srcStr1
* @param srcStr2
* @return
*/
public static String getMaxLenRepeatedSubStr(String srcStr1,String srcStr2){
if (srcStr1==null){
return null;
}
if (srcStr1.isEmpty()){
return null;
}
if (srcStr2==null){
return null;
}
if (srcStr2.isEmpty()){
return null;
}
Random random = new Random();
HashSet<String> stringHashSet = new HashSet<>();
int count=0;
while (true){
int srcStr1SubStrBegIndex= random.nextInt(srcStr1.length());
int srcStr1SubStrEndIndex= random.nextInt(srcStr1.length());
int srcStr1SubStrEndIndex1=srcStr1SubStrEndIndex 1;
if (srcStr1SubStrBegIndex<srcStr1SubStrEndIndex1){
String substring = srcStr1.substring(srcStr1SubStrBegIndex, srcStr1SubStrEndIndex1);
stringHashSet.add(substring);
}
if (count>1000000){
break;
}
count ;
}
HashSet<String> stringHashSet1 = new HashSet<>();
int count1=0;
while (true){
int srcStr2SubStrBegIndex= random.nextInt(srcStr2.length());
int srcStr2SubStrEndIndex= random.nextInt(srcStr2.length());
int srcStr2SubStrEndIndex1=srcStr2SubStrEndIndex 1;
if (srcStr2SubStrBegIndex<srcStr2SubStrEndIndex1){
String substring = srcStr2.substring(srcStr2SubStrBegIndex, srcStr2SubStrEndIndex1);
stringHashSet1.add(substring);
}
if (count1>1000000){
break;
}
count1 ;
}
ArrayList<String> stringArrayList = new ArrayList<>();
stringArrayList.addAll(stringHashSet);
stringArrayList.addAll(stringHashSet1);
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
stringArrayList.forEach(e->{
if (stringIntegerHashMap.containsKey(e)){
stringIntegerHashMap.put(e,stringIntegerHashMap.get(e) 1);
}else {
stringIntegerHashMap.put(e,1);
}
});
ArrayList<CustStrCompute3> stringArrayList1 = new ArrayList<>();
stringIntegerHashMap.forEach((key,val)->{
if (val==2){
CustStrCompute3 custStrCompute3 = new CustStrCompute3();
custStrCompute3.setRepeatedStr(true);
custStrCompute3.setId(UUID.randomUUID().toString());
custStrCompute3.setRepeatedSubStr(key);
custStrCompute3.setRepeatedStrLen(key.length());
stringArrayList1.add(custStrCompute3);
}
});
Collections.sort(stringArrayList1, new Comparator<CustStrCompute3>() {
@Override
public int compare(CustStrCompute3 o1, CustStrCompute3 o2) {
return o1.getRepeatedStrLen()-o2.getRepeatedStrLen();
}
});
System.out.println(stringArrayList1);
return stringArrayList1.get(stringArrayList1.size()-1).getRepeatedSubStr();
}
}
class CustStrCompute3{
private String id;
private String repeatedSubStr;
private Boolean isRepeatedStr;
private Integer repeatedStrLen;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRepeatedSubStr() {
return repeatedSubStr;
}
public void setRepeatedSubStr(String repeatedSubStr) {
this.repeatedSubStr = repeatedSubStr;
}
public Boolean getRepeatedStr() {
return isRepeatedStr;
}
public void setRepeatedStr(Boolean repeatedStr) {
isRepeatedStr = repeatedStr;
}
public Integer getRepeatedStrLen() {
return repeatedStrLen;
}
public void setRepeatedStrLen(Integer repeatedStrLen) {
this.repeatedStrLen = repeatedStrLen;
}
@Override
public String toString() {
return "CustStrCompute3{"
"id='" id '''
", repeatedSubStr='" repeatedSubStr '''
", isRepeatedStr=" isRepeatedStr
", repeatedStrLen=" repeatedStrLen
'}';
}
}