2nd post on this question, better one


  • 0
    public class Solution {
        public boolean repeatedSubstringPattern(String s) {
            if (s == null || s.length() < 2) return false;
            int n = s.length();
            for (int len = 1; (len <= n / 2); len++) {
            	if (n % len != 0) continue;
            	String substr = s.substring(0, len);
            	if (helper(s, substr, len)) return true;
            }
            return false;
        }
        private boolean helper(String s, String p, int len) {
            int i = len;
            for (;i < s.length(); i+= len) {
                String substr = s.substring(i, i + len);
                if (!substr.equals(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.