力扣 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;
    }
}
相关推荐
niyongsheng1 分钟前
3D 装箱算法工程化:数据结构、放置启发式与支撑规则
数据结构·算法·rust
j7~3 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
傅科摆 _ py3 小时前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法
Shaoshing4 小时前
LeetCode100 42.接雨水
java·算法·leetcode
域限奇点访客4 小时前
用AI生成问卷的网站怎么选?看题目录入结构、题型映射和生成后的校验
算法
soft_hub15 小时前
海外会议怎么用录音APP翻译会议内容
面试·职场和发展
文丑颜不良啊5 小时前
LeetCode1261~1280题目解析
java·leetcode
not coder5 小时前
一文讲通--从树到红黑树
数据结构·算法
octopus_c5 小时前
C++ 入门
开发语言·c++·算法
ZhengEnCi6 小时前
2026年10月30日AI早报:Gemini 4 Carbon登顶、DeepSeek融资1055亿、智元机器人上岗实测
人工智能·算法