java O(N2) solution


  • 0
    M
    
        public boolean repeatedSubstringPattern(String str) {
            
        	int s_len = str.length();
        	
        	if(s_len<2)
        	    return false;
            
            for(int i=1; i<str.length(); i++){
                String sub = str.substring(0,i);
                int len = sub.length();
                if(s_len%len!=0) continue;
                boolean cc = check(str,sub);
                if(cc)
                    return true;
    
            }
            return false;
        
        }
        
        public boolean check(String str, String sub){
            int len = sub.length();
            int s_len = str.length();
            for(int j=0; j<s_len; j++){
                if(str.charAt(j)!=sub.charAt(j%len)) return false;
            }
            
            return true;
        }
    
    

Log in to reply
 

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