Ordinary method


  • 0
    W
    public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        int length = str.length();
        for(int i = 1; i < length; i++){
            if(length % i == 0){
                if(CheckRepeat(str, i)){
                    return true;
                }
            }
        }
        return false;
    }
    public boolean CheckRepeat(String str, int i){
        int div = str.length()/i;
        String s = str.substring(0, i);
        for(int index = 1; index < div; index++){
            int left = index*i;
            int right = (index + 1)*i;
            String temp = str.substring(left, right);
            if(!s.equals(temp)){
                return false;
            }
        }
        return true;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.