java 459.重复子串模式(KMP).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 459.重复子串模式(KMP).java相关的知识,希望对你有一定的参考价值。

public class Solution {
   public boolean repeatedSubstringPattern(String str) {
	int l = str.length();
	for(int i=l/2;i>=1;i--) {
		if(l%i==0) {
			int m = l/i;
			String subS = str.substring(0,i);
			StringBuilder sb = new StringBuilder();
			for(int j=0;j<m;j++) {
				sb.append(subS);
			}
			if(sb.toString().equals(str)) return true;
		}
	}
	return false;
}
}
public class Solution {
    private int[] getPrefix(String pattern) {
        int len = pattern.length();
        int[] lps = new int[len];
        lps[0] = 0;
        int matched = 0;
        for (int i = 1; i < len; i++) {
            while (matched > 0 && pattern.charAt(i) != pattern.charAt(matched)) {
                matched = lps[matched - 1];
            }
            if (pattern.charAt(i) == pattern.charAt(matched)) {
                matched++;
            }
            lps[i] = matched;
        }
        return lps;
    }
    public boolean repeatedSubstringPattern(String s) {
        int[] prefixFunction = getPrefix(s);
        int len = s.length();
        int last = prefixFunction[len - 1];
        return last != 0 && (len % (len - last) == 0);
    }
}

以上是关于java 459.重复子串模式(KMP).java的主要内容,如果未能解决你的问题,请参考以下文章

java 459.重复子串模式(KMP).java

java 459.重复子串模式(KMP).java

java 459.重复子串模式(KMP).java

java 459.重复子串模式(KMP).java

459.(KMP)求字符串是否由模式重复构成 Repeated Substring Pattern

LeetCode459. 重复的子字符串,KMP+暴力双循环