"abcwerthelloyuiodef"
"cvhellobnm"
思路:
1,將短的那個(gè)子串按照長度遞減的方式獲取到。
2,將每獲取到的子串去長串中判斷是否包含,如果包含,已經(jīng)找到!
class StringTest3{ public static String getMaxSubString(String s1,String s2) { String max = "",min = ""; max = (s1.length()>s2.length())?s1: s2; min = (max==s1)?s2: s1; // sop("max="+max+"...min="+min); for(int x=0; x<min.length(); x++) { for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++) { String temp = min.substring(y,z); sop(temp); if(max.contains(temp))//if(s1.indexOf(temp)!=-1) return temp; } } return ""; } public static void main(String[] args) { String s1 = "ab"; String s2 = "cvhellobnm"; sop(getMaxSubString(s2,s1)); } public static void sop(String str) { System.out.println(str); }}
以上這篇Java獲取兩個(gè)字符串中最大相同子串的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選