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

解题思路:

KMP算法

len - next[len - 1]作为最小公共子串的长度

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

注意:

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

next[len - 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 分钟前
leetcodehot100-347. 前 K 个高频元素
数据结构·算法·leetcode
玛丽莲茼蒿1 分钟前
Leetcode hot100 多数元素【简单】
算法·leetcode·职场和发展
AbandonForce2 分钟前
Map类:pair键值对|map的基本操作|operator[]
开发语言·c++·算法·leetcode
澈2074 分钟前
C++核心:封装与static静态成员实战指南
开发语言·c++·算法
田梓燊8 分钟前
力扣:146.LRU 缓存
算法·leetcode·缓存
_深海凉_15 分钟前
LeetCode热题100-杨辉三角
算法·leetcode·职场和发展
小O的算法实验室25 分钟前
2025年SEVC,面向进化计算的学习注入式优化,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
啊我不会诶34 分钟前
2024北京市赛补题
c++·算法
shehuiyuelaiyuehao35 分钟前
算法13,滑动窗口,水果成篮
算法·哈希算法·散列表
智慧物业老杨35 分钟前
物业数智化转型实战:从单一服务到综合解决方案的技术落地路径
人工智能·算法·ai