题解 | #最长重复子串#
最长重复子串
http://www.nowcoder.com/practice/4fe306a84f084c249e4afad5edf889cc
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param a string字符串 待计算字符串
* @return int整型
*/
public int solve (String a) {
// write code here
int maxLen = 0;
for(int i = 0; i < a.length(); i++){
for(int j = i + 2; j < a.length() + 1; j+=2){
StringBuilder sb = new StringBuilder(a.substring(i,j));
// if(sb.length() % 2 == 0){
int halfIndex = sb.length() / 2;
if(sb.substring(0,halfIndex).equals(sb.substring(halfIndex,sb.length())) && sb.length() > maxLen){
maxLen = sb.length();
}
// }
}
}
return maxLen;
}
}