题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.next(); String s2 = in.next(); if(s1.length() > s2.length()){ String temp = s1; s1 = s2; s2 = temp; } boolean flag = false; for(int len = s1.length(); len>=1 ;len--){ for(int l = 0; l+len <= s1.length(); l++){ String temp = s1.substring(l,l+len); if(s2.contains(temp)){ System.out.println(temp); flag = true; break; } } if(flag) break; } } }