题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str1 = in.nextLine(); String str2 = in.nextLine(); int len1 = str1.length(); int len2 = str2.length(); String minString = len1 < len2 ? str1 : str2; String maxString = len1 < len2 ? str2 : str1; int len = Math.min(len1, len2); boolean flag = true; for (int i = len; i >= 0; i--) { for (int j = 0; j + i <= len; j++) { String subString = minString.substring(j, j + i); if (maxString.contains(subString)) { System.out.println(i); flag = false; break; } } if (flag == false) break; } } }