import java.util.*; import java.lang.String; public class Solution { /** * longest common substring * @param str1 string字符串 the string * @param str2 string字符串 the string * @return string字符串 */ public String LCS (String str1, String str2) { // write code here String res = ""; for(int i = 0;i< str1.length();i++){ char next = str1.charAt(i); //从另一个字符串中找到这个 for(int j = 0;j< str2.length();j++){ char next2 = str2.charAt(j); if(next == next2){ //找到后去计算最大长度 可能找到多个 要继续遍历 res = calculate(str1,i,str2,j,res); } } } return res; } public String calculate(String str1,int i,String str2,int j,String res){ String cur = ""; while(i < str1.length() && j < str2.length()){ if(str1.charAt(i) == str2.charAt(j)){ cur += str1.charAt(i); } else { break; } i ++; j ++; } if(cur.length() > res.length()){ return cur; } else { return res; } } }