686. 重复叠加字符串匹配

给定两个字符串 a 和 b,寻找重复叠加字符串 a 的最小次数,使得字符串 b 成为叠加后的字符串 a 的子串,如果不存在则返回 -1。

注意:字符串 "abc" 重复叠加 0 次是 "",重复叠加 1 次是 "abc",重复叠加 2 次是 "abcabc"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/repeated-string-match
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

KMP

class Solution {

    private int[] getNext(String str) {
        int[] next = new int[str.length()];
        next[0] = -1;
        int i = 0, j = -1;
        while (i < str.length() - 1) {
            if (j == -1 || str.charAt(i) == str.charAt(j)) {
                if (str.charAt(++i) == str.charAt(++j)) {
                    next[i] = next[j];
                } else {
                    next[i] = j;
                }
            } else {
                j = next[j];
            }
        }
        return next;
    }

    public int repeatedStringMatch(String a, String b) {
        int[] next = getNext(b);
        int i = 0, j = 0;
        while (i - j < a.length() && j < b.length()) {
            if (j == -1 || a.charAt(i % a.length()) == b.charAt(j)) {
                i++;
                j++;
            } else {
                j = next[j];
            }
        }
        if (j != b.length()) {
            return -1;
        }
        return i % a.length() == 0 ? i / a.length() : i / a.length() + 1;
    }
}

Rabin-Karp

上一篇:力扣题301删除无效的括号


下一篇:394. 字符串解码