package leetcode;

/**
 * https://leetcode.com/problems/repeated-substring-pattern/
 */
public class Problem459 {
    public boolean repeatedSubstringPattern(String str) {
        if (str.isEmpty() || str.length() == 1) {
            return false;
        }
        String sub1 = "" + str.charAt(0);
        String sub2 = "";
        int i = 0;
        i++;
        while (i < str.length() && i + sub1.length() <= str.length()) {
            sub2 = str.substring(i, i + sub1.length());
            if (sub1.equals(sub2)) {
                i += sub1.length();
            } else {
                sub1 += str.charAt(i);
                i++;
            }
        }
        return sub1.equals(sub2) && i == str.length();
    }

    public static void main(String[] args) {
        Problem459 prob = new Problem459();
//        System.out.println(prob.repeatedSubstringPattern("abab")); // true
//        System.out.println(prob.repeatedSubstringPattern("aba")); // false
//        System.out.println(prob.repeatedSubstringPattern("abcabcabcabc")); // true
//        System.out.println(prob.repeatedSubstringPattern("abcabca")); // false
//        System.out.println(prob.repeatedSubstringPattern("")); // false
//        System.out.println(prob.repeatedSubstringPattern("abac")); // false
//        System.out.println(prob.repeatedSubstringPattern("a")); // false
//        System.out.println(prob.repeatedSubstringPattern("abababaaba")); // false
        System.out.println(prob.repeatedSubstringPattern("aaaabaaaab")); // true
    }
}

@@ -177,28 +177,28 @@
     
-if (str.isEmpty() %7C%7C
+for (int i = 1; i %3C=
  str
@@ -211,12 +211,16 @@
 h() 
-== 1
+/ 2; i++
 ) %7B%0A
@@ -235,20 +235,25 @@
     
-return false
+String sub = null
 ;%0A  
@@ -262,78 +262,38 @@
     
-%7D%0A
     
-    String sub1 = %22%22 + str.charAt(0);%0A        String sub2 = %22%22;%0A
+boolean found = true;%0A    
     
@@ -300,17 +300,17 @@
     int 
-i
+j
  = 0;%0A  
@@ -319,31 +319,27 @@
     
-i++;%0A        while (
+    for (; j + 
 i %3C
+=
  str
@@ -351,45 +351,16 @@
 th()
- && i + sub1.length() %3C= str.length()
+; j += i
 ) %7B%0A
@@ -371,20 +371,33 @@
         
-s
+    String tmpS
 ub
-2
  = str.s
@@ -409,31 +409,23 @@
 ing(
-i, i + sub1.length());%0A
+j, j + i);%0A    
     
@@ -439,30 +439,24 @@
  if (sub
-1.equals(sub2)
+ == null
 ) %7B%0A    
@@ -471,28 +471,30 @@
     
-i += sub1.length();%0A
+    sub = tmpSub;%0A    
     
@@ -530,31 +530,40 @@
     
-sub1 += str.charAt(i);%0A
+    if (!sub.equals(tmpSub)) %7B%0A 
     
@@ -578,26 +578,29 @@
     
-i++;%0A
        
-     %7D
+found = false;
 %0A   
@@ -608,18 +608,19 @@
     
-%7D%0A
+   
         
 retu
@@ -619,862 +619,194 @@
     
-return sub1.equals(sub2) && i == str.length()
+     break
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem459 prob = new Problem459();%0A//        System.out.println(prob.repeatedSubstringPattern(%22abab%22)); // true%0A//        System.out.println(prob.repeatedSubstringPattern(%22aba%22)); // false%0A//        System.out.println(prob.repeatedSubstringPattern(%22abcabcabcabc%22)); // true%0A//        System.out.println(prob.repeatedSubstringPattern(%22abcabca%22)); // false%0A//        System.out.println(prob.repeatedSubstringPattern(%22%22)); // false%0A//        System.out.println(prob.repeatedSubstringPattern(%22abac%22)); // false%0A//        System.out.println(prob.repeatedSubstringPattern(%22a%22)); // false%0A//        System.out.println(prob.repeatedSubstringPattern(%22abababaaba%22)); // false%0A        System.out.println(prob.repeatedSubstringPattern(%22aaaabaaaab%22)); // true
+            %7D%0A                %7D%0A            %7D%0A            if (found && j == str.length()) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;
 %0A   

Solve problem 459