Java Readable Solution 25ms


  • 0
    K
    public class Solution {
        public boolean repeatedSubstringPattern(String str) {
            for (int i = 0; i < str.length() / 2; i ++) {
                // check to make sure the length of the substring
                // is a factor of the length of the string
                if (str.length() % (i + 1) != 0) {
                    continue;
                }
                
                int len = i + 1;
                String substring = str.substring(0, len);
                
                boolean success = true;
                for (int j = len; success && ((j + len) <= str.length()); j += len) {
                    String test = str.substring(j, j + len);
                    success &= test.equals(substring);
                }
                
                if (success) {
                    return true;
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

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