Java 15ms Solution


  • 0
    V
    public class Solution {
        public boolean repeatedSubstringPattern(String str) {
            if(str.length() == 1)
                return false;
            char[] ar = str.toCharArray();
            int len = ar.length;
            for(int i = len/2; i >=1 ; i--) {
                if(ar[i] == ar[0]) {
                    if(len % i == 0) {
                        String temp = str.substring(0, i);
                        for(int j = i; j < len;) {
                            if(str.substring(j, j + i).equals(temp))
                                j = j + i;
                            else
                                break;
                            if(j == 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.