Repeated Substring Pattern    

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"

Output: True

Explanation: It's the substring "ab" twice.

 

Example 2:

Input: "aba"

Output: False

 

Example 3:

Input: "abcabcabcabc"

Output: True

Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        if(s == null || s.length() == 0) return false;
        int j = 1;
        for(; j * 2 <= s.length(); j++){
            System.out.println(s.charAt( j ));
            if(s.charAt(0) == s.charAt(j) && s.length() % j == 0 && s.substring(0, j).equals( s.substring(j, j * 2) )) {
                for(int i = 0; i + j <= s.length(); i += j ){
                    if(!s.substring(i, i + j).equals( s.substring(0, j))) break;
                    if(i + j == s.length()) return true;
                }

            }
        }
        return false;
    }
}

  

KMP方法的next数组,next数组的划分都是相等的整数倍,即true 否则为false

相关文章:

  • 2021-11-04
  • 2021-08-10
  • 2022-02-07
  • 2021-10-16
  • 2021-12-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案