Java获取两个字符串中最大相同子串的方法
发布时间:2020-12-14 19:46:57 所属栏目:Java 来源:网络整理
导读:"abcwerthelloyuiodef" "cvhellobnm" 思路: 1,将短的那个子串按照长度递减的方式获取到。 2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到! class StringTest3{public static String getMaxSubString(String s1,String s2){String max = ""
"abcwerthelloyuiodef" "cvhellobnm" 思路: 1,将短的那个子串按照长度递减的方式获取到。 2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到! 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获取两个字符串中最大相同子串的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |