代码随想录二刷第八天 | 28. 实现 strStr()、459.重复的子字符串

28. 实现 strStr()

题目:28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode)

题解:代码随想录

状态:需要多复习,多看

思路

KMP算法,实现方式为next数组初始化为0,不减一

代码

时间复杂度:O(m + n) 空间复杂度:O(m)

java 复制代码
class Solution {
    public int strStr(String haystack, String needle) {
        if(needle.length() == 0) return 0;
        if(needle.length() > haystack.length()) return -1;

        int[] next = new int[needle.length()];
        getNext(next, needle);

        int j = 0;
        for(int i = 0; i < haystack.length(); i++){
            while(j > 0 && haystack.charAt(i) != needle.charAt(j)){
                j = next[j - 1];
            }
            if(haystack.charAt(i) == needle.charAt(j)){
                j++;
            }
            if(j == needle.length()){
                return i - j + 1;
            }
        }

        return -1;
    }

    private static void getNext(int[] next, String s){
        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;
        }
    }
}

459.重复的子字符串

题目:459. 重复的子字符串 - 力扣(LeetCode)

题解:代码随想录

状态:需要结合KMP多看

思路

KMP算法+移动匹配。abcabc+abcabc,去掉开头和结尾,在其中找abcabc

代码

时间复杂度:O(m + n) 空间复杂度:O(m)

java 复制代码
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        return kmp(s+s, s);
    }

    private boolean kmp(String str, String pattern){
        int[] next = new int[pattern.length()];
        next[0] = 0;
        int j = 0;
        for(int i = 1; i < pattern.length(); i++){
            while(j > 0 && pattern.charAt(i) != pattern.charAt(j)){
                j = next[j - 1];
            }
            if(pattern.charAt(i) == pattern.charAt(j)){
                j++;
            }
            next[i] = j;
        }

        j = 0;
        for(int i = 1; i < str.length() - 1; i++){
            while(j > 0 && str.charAt(i) != pattern.charAt(j)){
                j = next[j - 1];
            }
            if(str.charAt(i) == pattern.charAt(j)){
                j++;
            }
            if(j == pattern.length()){
                return true;
            }
        }

        return false;
    }
}
相关推荐
夕除7 分钟前
redis--主从还是集群,内存
java·spring·mybatis
百数平台10 分钟前
低代码安全技术实践:表单 / 流程 / 报表三级权限体系设计解析
java·大数据·数据库
白露与泡影13 分钟前
Spring Boot 实现验证码:Hutool-captcha、AJ-Captcha、EasyCaptcha 到底怎么选?
java·spring boot·后端
马优晨22 分钟前
Java中的dto是什么
java·开发语言·java中的dto是什么·java的dto是什么·dto在java中是什么
小蒜学长27 分钟前
基于Springboot+Vue的高校教师教学业绩综合管理系统的设计与开发(代码+数据库+LW)
java·数据库·spring boot·后端·教学业绩管理·三级权限
Mikko729 分钟前
Spring Boot 3.5.16 自带的 Tomcat 10.1.55 中了哪些 CVE?其中一条是修上一个漏洞时带进来的
java·spring boot·后端·安全·tomcat
十年Java程序媛33 分钟前
Java 内部类全解|成员 / 静态 / 匿名内部类,理清和 Lambda 的区别
java·spring boot·面试
SL_staff1 小时前
报工≠打卡:从任务报工到闭环管控的5个数据断点排查清单
java·spring boot·全栈
tryxr1 小时前
如何优化sql
java·数据库·sql·数据库优化
SamDeepThinking1 小时前
配货单发货数为空告警误报治理:写表延迟复核方案
java·后端·架构