题解 | #最长重复子串#
最长重复子串
https://www.nowcoder.com/practice/4fe306a84f084c249e4afad5edf889cc
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param a string字符串 待计算字符串 * @return int整型 */ public int solve (String a) { // write code here if (a == null || a.length() < 1) { return 0; } char[] arr = a.toCharArray(); int n = arr.length; int max = 0; for (int i = n / 2; i > 0; i--) { for (int j = 0; j <= n - 2 * i; j++) { if (check(arr, i, j)) { return 2 * i; } } } return 0; } private boolean check(char[] arr, int len, int begin) { for (int i = begin; i < len + begin; i++) { if (arr[i] != arr[i + len]) { return false; } } return true; } }