LeetCode | 28. Find the Index of the First Occurrence in a String, 459. Repeated Substring Pattern

文章介绍了如何使用KMP算法来解决两个字符串问题:一是找到给定字符串中指定子串的首次出现索引,二是判断一个字符串是否能通过自身的子串重复连接形成。KMP算法在处理这类问题时避免了多余的比较,提高了效率。
摘要由CSDN通过智能技术生成

28. Find the Index of the First Occurrence in a String

Link: https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/

Description

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Approach

KMP

Link: https://programmercarl.com/0028.%E5%AE%9E%E7%8E%B0strStr.html

Solution

class Solution {
    public int strStr(String haystack, String needle) {
        if (needle.length() == 0)
            return 0;
        int[] next = new int[needle.length()];
        getNext(needle, next);
        int j = -1;
        for (int i = 0; i < haystack.length(); i++) {
            while (j >= 0 && haystack.charAt(i) != needle.charAt(j + 1))
                j = next[j];
            if (haystack.charAt(i) == needle.charAt(j + 1))
                j++;
            if (j == needle.length() - 1)
                return i - (needle.length() - 1);
        }
        return -1;
    }

    private void getNext(String s, int[] next) {
        int j = -1;
        next[0] = -1;
        for (int i = 1; i < s.length(); i++) {
            while (j >= 0 && s.charAt(i) != s.charAt(j + 1))
                j = next[j];
            if (s.charAt(i) == s.charAt(j + 1))
                j++;
            next[i] = j;
        }
    }
}

459. Repeated Substring Pattern

Link: https://leetcode.com/problems/repeated-substring-pattern/

Description

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Approach

KMP

Link: https://programmercarl.com/0459.%E9%87%8D%E5%A4%8D%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2.html

移动匹配

Link: https://programmercarl.com/0459.%E9%87%8D%E5%A4%8D%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2.html

Solution

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        String s2 = s + s;
        String s1 = s2.substring(1, s2.length() - 1);
        System.out.println(s1);
        if (s1.contains(s))
            return true;
        else 
            return false;
    }
}
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        if (s.length() == 0)
            return false;
        int[] next = new int[s.length()];
        getNext(s, next);
        for (int i: next)
            System.out.println(i);
        if (next[s.length() - 1] >= 0 && s.length() % (s.length() - (next[s.length() - 1] + 1)) == 0)
            return true;
        else
            return false;
    }

    private void getNext(String s, int[] next) {
        int j = -1;
        next[0] = -1;
        for (int i = 1; i < s.length(); i++) {
            while (j >= 0 && s.charAt(i) != s.charAt(j + 1))
                j = next[j];
            if (s.charAt(i) == s.charAt(j + 1))
                j++;
            next[i] = j;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值