Intuitive Java Solution beats 70%


  • 0
    Z
    public class Solution {
        public boolean repeatedSubstringPattern(String str) {
            if(str.length() == 1)
                return false;
            int len = str.length();
            StringBuffer sb = new StringBuffer(str);
            char c = sb.charAt(0);
            for(int i = 1; i <= len / 2; i++){
                if(c == sb.charAt(i)){
                    int slen = i;                       // Pattrn length;
                    if(len % slen == 0){
                        String pattern = sb.substring(0, slen); 
                        for(int j = slen ; j + slen <= len; j += slen){
                            if(!sb.substring(j, j + slen).equals(pattern))
                                break;
                            if(j + slen == len)
                                return true;
                        }
                    } 
                }
            }
            return false;
        }
    }
    

Log in to reply
 

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