[黑马论坛]23期技术活动题目及答案!
-------------23期限技术题:
题目:已知两个字符串,String a = "abcdefghijklmnopqrstuvwxyz";String b="1234567890abcdfrgtddd12321414";编写代码求出这两个字符串中最大公共字符串(最大公共字符串:比如 “abc123edf”和"bc123jg"的最大公共字符串是“bc123”)。上传代码的时候将运算结果截图一起提交。
-------------答案:
package com.itheima.practice;
public class MaxLenghtStringIntersection {
/**
* @param args
*/
public static void main(String[] args) {
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "1234567890abcdfrgtddd12321414";
String result = getMaxLenghtStringIntersection(a, b);
System.out.println(result);
}
private static String getMaxLenghtStringIntersection(String a, String b) {
String maxLenStr = null;
String minLenStr = null;
maxLenStr = (a.length() > b.length())? a : b;
minLenStr = maxLenStr.equals(a) ? b : a;
for (int i = 0; i < minLenStr.length(); i++) {
for (int start = 0, end = minLenStr.length() - i; end != minLenStr.length() + 1; start++, end++) {
String sub = minLenStr.substring(start, end);
if (maxLenStr.contains(sub))
return sub;
}
}
return null;
}
}