力扣 LeetCode 459. 重复的子字符串(Day4:字符串)

解题思路:

KMP算法

len - nextlen - 1作为最小公共子串的长度

len % (len - nextlen - 1) == 0检测能否构成重复串,能构成整数倍,代表可以构成

注意:

i 从 j 的下一位开始,即 i 初始化为 1

nextlen - 1需要大于0(等于0时,len%len一定满足==0,未起到判断效果,因为一定返回true)

java 复制代码
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int len = s.length();
        int[] next = new int[len];
        int j = 0;
        next[0] = 0;
        for (int i = 1; i < s.length(); i++) {
            while (j > 0 && s.charAt(i) != s.charAt(j)) j = next[j - 1];
            if (s.charAt(i) == s.charAt(j)) j++;
            next[i] = j;
        }
        if (next[len - 1] > 0 && len % (len - next[len - 1]) == 0) return true;
        return false;
    }
}
相关推荐
晴天的雨.9924 分钟前
[C++算法]快乐数
数据结构·c++·算法
孤狼warrior8 分钟前
SCTR 五次失败的安全 BN 路由器
人工智能·python·深度学习·算法·安全·yolo
影视飓风TIM10 分钟前
C++11 核心新特性完整梳理
数据结构·c++·算法
晴天的雨.99211 分钟前
[C++]算法双指针 复写0
数据结构·c++·算法
橘子汽水16825 分钟前
Leetcode 128,49最长连续序列,字母异位词分组
java·算法·leetcode
圣保罗的大教堂27 分钟前
leetcode 1140. 石子游戏 II 中等
leetcode
mmmmath_327 分钟前
LeetCode.438.找到字符串中所有字母异位词
数据结构·算法·leetcode
Nil20832 分钟前
leetcode 22括号生成
算法·leetcode·深度优先
Navigator_Z37 分钟前
LeetCode //C - 1237. Find Positive Integer Solution for a Given Equation
c语言·算法·leetcode