import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 给定一个字符串s,返回具有相同前缀后缀的子串的第二大长度,反之,返回-1即可。
* @param s string字符串 代表题意中的字符串s
* @return int整型
*/
public static int solve (String s) {
int temp=0;
int l=s.length();
for (int i = 1; i < l; i++) {
if(s.charAt(temp)==s.charAt(i)){
temp++;
}else{
if(s.charAt(i)==s.charAt(0))temp=1;
else temp=0;
}
}
if(temp==0) return -1;
return temp;
}
}"ababcabababc"这个结果应该是5,上面跑的是-1,这代码是对的?

京公网安备 11010502036488号