A Simple Java Solution 36ms


  • 0
    N
    public class Solution {
        public boolean repeatedSubstringPattern(String str) {
            if(str.length() < 2) 
                return false;
            int first = 0 , second = 1;
            int times = 0;
            while(second < str.length()){
                if(str.charAt(first) == str.charAt(second)){   
                	times = second - first;
                    for(int flag = 1;str.charAt(first) == str.charAt(second);first++,second++,flag++){
                        if(second == str.length() -1 && flag%times == 0)
                            return true;
                        if(second == str.length() -1 && flag%times != 0)
                            return false;
                    }
                    first = 0;
                }
                else{
                    second++;
                }
            }
            return false;
        }
    }
    

Log in to reply
 

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