取两个字符串中出现的最大字串

public class StringTest3 {
public static void main(String[] args) {
String str1 = "helabchaahha";
String str2 = "boodoabcdworldhhha";
String str3 = getMaxStr(str1,str2);
System.out.println(str3);
}
public static String getMaxStr(String s1,String s2) {
String min=null,max=null;
max = (s1.length()>s2.length())?s1:s2;
min = (max.equals(s1))?s2:s1;
for(int x=0;x<min.length();x++) {
for(int m=0,n=min.length()-x;n!=min.length()+1;m++,n++) {
String s3 = max.substring(m, n);
//System.out.println(s3);
if(s1.contains(s3)) {
return max+" "+min+"\n"+"最大的子串为"+s3;
}
}
}
return null;

}
}

时间: 2024-07-31 17:07:04

取两个字符串中出现的最大字串的相关文章

java基础知识回顾之---java String final类普通方法的应用之“两个字符串中最大相同的子串”

/* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" *  * 思路: * 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中. *   如果存在,短的那个字符串就是最大子串. * 2,如果不是呢,那么就将短的那个子串进行长度递减的方式取子串,去长串中判断是否存在. *   如果存在就已找到,就不用在找了. * 3.先找最大的子串,再递减子串找,找到,就停止 */ 原理图如图: 代码: publi

获取两个字符串中最大相同子串

2.获取两个字符串中最大相同子串.第一个动作:将短的那个串进行长度一次递减的子串打印. "cvhellobnmtanop" "andefc" 思路: 1,将短的那个子串按照长度递减的方式获取到. 2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到! package tan; class Test { public static String getMaxSubString(String s1,String s2) { String max = "

两个字符串中最大相同的子串

/* * 两个字符串中最大相同的子串. *  *  * * 思路: * 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中. * 如果存在,短的那个字符串就是最大子串. * 2,如果不是呢,那么就将短的那个子串进行长度递减的方式去子串,去长串中判断是否存在. * 如果存在就已找到,就不用在找了. */ public class Test2 { public static void main(String[] args) { // TODO 自动生成的方法存根 String s1="

给定两个字符串,获取两个字符串中最大相同的子串

1 package weekpratisce; 2 3 ///给定两个字符串,获取两个字符串中最大相同的子串 4 public class Demo9 { 5 public static void main(String[] args) { 6 String xx = "aaaaaaaaaaddddddd", yy = "45ddddda"; 7 String str = getMaxsubstring(xx, yy); 8 System.out.println(s

取出两个字符串中最大相同的子串

//************************************************************************* //题目要求:4.取出两个字符串中最大相同的子串. //************************************************************************* public class SearchMaxSameString { public static void main(String[] args

找出两个字符串中最长的相同子字符串

//找出两个字符串中最长的相同子字符串 public class Stringdemo { public static void main(String[] args) { String str1 = new String("eeabcde"); String str2 = new String("bcdefabcabcdedegg"); byte[] char1 = str1.getBytes(); byte[] char2 = str2.getBytes();

Oracle 取两个表中数据的交集并集差异集合

Oracle 取两个表中数据的交集 关键字: Oracle 取两个表中数据的交集 INTERSECT Oracle 作为一个大型的关系数据库,日常应用中往往需要提取两个表的交集数据 例如现有如下表,要求找出工资2500(不含2500)以上并且是男性(M)的员工编号,那么就要利用这两个表的关系做一个交集了 employee CODE NAME GENDER 001 Tom M 002 Jerry M 003 Ana F salary CODE SALARY 001 2800 002 2500 00

获取两个字符串中最大的相同子串

public class 获取两个字符串中最大的相同子串 { public static void main(String[] args) { String a="abcwerthelloadcedf"; String b="cdhelloesadcedf"; String c=getSonString(a,b); System.out.println(c); } private static String getSonString(String a, String

黑马程序员——找出两个字符串中最大的子串

找出两个字符串中最大的子串 </pre><pre name="code" class="java">public class StringMaxString { //找一个字符串的最大子串 public static void main(String[] args) { // TODO Auto-generated method stub String s1="qwerabcdtyuiop"; String s2=&quo