问题详述
获取两个字符串中最大相同子串。
比如:str1 = “abcwerthelloyuiodef“;str2 = “cvhellobnm”
最大相同子串是"hello”
实现思路
str1 = "abcwerthelloyuiodef“;str2 = “cvhellobnm”
str1是否包含str2?
str2长度减1----------str1是否包含 “cvhellobnm”、 “cvhellobnm”
str2长度减2----------str1是否包含 "cvhellobnm "、 "cvhellobnm "、 “cv hellobnm”
str3长度减3----------str1是否包含"cvhellobnm "、"c vhellobnm "、"cv hellobnm "、“cvh ellobnm”
…
…
遇到第一个符合条件的,就是最长子串,返回它,结束方法,后面更短的就不用看了
代码实现
import org.junit.Test;
public class SameString {
//两个字符串的最大相同字串只有一个
public String maxSameString(<