Java solution O(n * #divisor(n))


  • 0
    D
    public class Solution {
        public boolean repeatedSubstringPattern(String s) {
            if (s == null || s.length() <= 1) {
                return false;
            }
            for (int e = 0; e < s.length() / 2; e++) {
                if (repeatedSubstringPattern(s, e)) {
                    return true;
                }
            }
            return false;
        }
        
        private boolean repeatedSubstringPattern(String s, int e) {
            int chunksize = e + 1;
            if ((s.length() % chunksize) != 0) {
                return false;
            }
            for (int chunk = 1; chunk < s.length() / chunksize; chunk++) {
                for (int p = 0; p <= e; p++) {
                    if (s.charAt(chunksize * chunk + p) != s.charAt(p)) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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