package com.zmz.algorithm.string;
import java.util.Arrays;

/**
 * @author 张明泽
 * Create by 2022/5/26 18:33
 * 重复的子字符串
 * LeetCode-459
 */
public class RepeatString {
    public static void main(String[] args) {
        String str = "abcabcabcabc";
        Boolean result = getNext(str);
        System.out.println(result);
    }

    /**
     * 如果重复 后面的子串一定是递增的...
     * 数组的最后一个元素是 最长的相同前缀后缀 长度
     */
    public static Boolean getNext(String str) {
        int[] next = new int[str.length()];
        int length = next.length;
        next[0] = 0;
        int left = 0;
        for (int right = 1; right < str.length(); right++) {
            while (left > 0 && str.charAt(left) != str.charAt(right)) {
                left = next[left - 1];
            }
            if (str.charAt(left) == str.charAt(right)) {
                left++;
            }
            next[right] = left;
        }
        System.out.println(Arrays.toString(next));
        if (next[length - 1] != 0 && length % (length - next[length - 1]) == 0) {
            return true;
        } else {
            return false;
        }
    }

}
最后修改:2022 年 05 月 27 日 07 : 13 AM
赏杯咖啡喝 谢谢您~