package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Max_Substring_Test { public static void main(String[] args) { /* * 几个字符串的最大公共子字符串 */ String str1 = "eriousdfndnfdlk"; String str2 = "dkgfdkgkdjgdjgjksjgksgjsdkjsdierioe"; String str3 = "ldskfjdskierundsnfjerioejrejrheruer3j43j4hj3"; String sub; List<String> list=new ArrayList<String>(); for (int i = 0; i < str1.length(); i++) { sub = str1.substring(0, i); if(str2.indexOf(sub)!=-1&&str3.indexOf(sub)!=-1) { // System.out.println(sub); list.add(sub); } } Collections.sort(list, new Comparator<String>() { @Override public int compare(String arg0, String arg1) { int i=arg1.length()-arg0.length(); return i; } }); System.out.println("The max subString is "+list.get(0)); } }
原文地址:https://www.cnblogs.com/qingyundian/p/8289900.html
时间: 2024-10-07 21:10:35