力扣 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;
    }
}
相关推荐
碧海银沙音频科技研究院1 小时前
炬芯ATS3089X手表的HFP通话不自动路由到设备BT的问题解决
网络·人工智能·嵌入式硬件·算法
mmmmath_36 小时前
LeetCode.199.二叉树的右视图
数据结构·leetcode
专注仿真6 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
码爸7 小时前
支持向量机介绍与示例
算法·机器学习·支持向量机
怕浪猫7 小时前
2028编程圈很火的10个Skills
算法·面试·github
Nil20811 小时前
leetcode 198打家劫舍
算法·leetcode·职场和发展
Shaoshing12 小时前
前缀和与差分
java·数据结构·算法·前缀和·差分
mmmmath_312 小时前
LeetCode.429.N叉树的层序遍历
数据结构·leetcode
loulanyue_12 小时前
构建房产认知决策智能:Harness底座与AI自进化的技术实践——读贝壳首席算法科学家尹俊希2026云栖专场演讲
人工智能·算法