你没考虑两个字符串一样长的问题,修改成一下就行 import java.util.*; public class Main { public static void main(String [] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); String s2 = sc.nextLine(); System.out.println(getPublicStr(s1, s2)); } public static int getPublicStr( String s1, String s2) { int count = 0; for (int i = 0; i < s1.length(); i++) { for (int j = s1.length(); j > i; j--) { if (s2.contains(s1.substring(i, j))) { count = Math.max(j - i, count); } } } return count; } }
点赞

相关推荐

已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
用微笑面对困难:这里面最强的是驾驶证了,可以入职美团大厂,然后直接开启黄马褂人生
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务