力扣 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;
    }
}
相关推荐
Fuly10243 分钟前
技术经理面试相关--技术篇
面试·职场和发展
嫩萝卜头儿5 分钟前
2 - 复杂度收尾 + 链表经典OJ
数据结构·算法·链表·复杂度
星马梦缘17 分钟前
算法设计与分析 作业二 答案与解析
算法·图论·dfs·bfs·floyd-warshall·bellman_ford·多源最短路
玛丽莲茼蒿18 分钟前
Leetcode hot100 每日温度【中等】
算法·leetcode·职场和发展
cjp56026 分钟前
009.UG二次开发,任务环境草图优化3(高级功能生成直线)
算法
样例过了就是过了37 分钟前
LeetCode热题100 分割等和子集
数据结构·c++·算法·leetcode·动态规划
逻辑驱动的ken39 分钟前
Java高频面试考点18
java·开发语言·数据库·算法·面试·职场和发展·哈希算法
北顾笙9801 小时前
day38-数据结构力扣
数据结构·算法·leetcode
m0_629494731 小时前
LeetCode 热题 100-----14.合并区间
数据结构·算法·leetcode
xin_nai1 小时前
LeetCode热题100(Java)(5)普通数组
算法·leetcode·职场和发展