货拉拉考过
利用双指针
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param a string字符串 待计算字符串
* @return int整型
*/
public int solve (String a) {
// write code here
int max = 0;
for (int i = 1; i < a.length(); i++) {
int count = 0;
for (int j = 0; j < a.length() && j + i < a.length(); j++) {
if (a.charAt(j) == a.charAt(j + i)) count++;
}
if (i == count) max = Math.max(max, count);
}
return max * 2;
}
}
京公网安备 11010502036488号