package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/palindrome-partitioning/
 */
public class Problem131 {
    public List<List<String>> partition(String s) {
        List<List<String>> result = new ArrayList<>();
        List<String> accu = new ArrayList<>();
        partition(s, accu, result);
        return result;
    }

    private void partition(String s, List<String> accu, List<List<String>> result) {
        if (s.length() == 0) {
            result.add(accu);
            return;
        }
        for (int i = 0; i < s.length(); i++) {
            String leftSubstr = s.substring(0, i + 1);
            if (!isPalindrome(leftSubstr)) {
                continue;
            } else {
                List<String> newAccu = new ArrayList<>(accu);
                newAccu.add(leftSubstr);
                String rightSubstr = s.substring(i + 1);
                partition(rightSubstr, newAccu, result);
            }
        }
    }

    private boolean isPalindrome(String s) {
        char[] chars = s.toCharArray();
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (chars[left++] != chars[right--]) {
                return false;
            }
        }
        return true;
    }
}

@@ -235,22 +235,22 @@
 tring%3E%3E 
-result
+answer
  = new A
@@ -275,80 +275,49 @@
     
-List%3CString%3E accu = new ArrayList%3C%3E();%0A        partition(s, accu, result
+partition(s, 0, new ArrayList%3C%3E(), answer
 );%0A 
@@ -330,22 +330,22 @@
  return 
-result
+answer
 ;%0A    %7D%0A
@@ -357,16 +357,23 @@
 private 
+static 
 void par
@@ -388,16 +388,27 @@
 tring s,
+ int index,
  List%3CSt
@@ -438,22 +438,22 @@
 tring%3E%3E 
-result
+answer
 ) %7B%0A    
@@ -474,17 +474,21 @@
 th() == 
-0
+index
 ) %7B%0A    
@@ -499,24 +499,41 @@
     
-result.add
+answer.add(new ArrayList%3C%3E
 (accu)
+)
 ;%0A  
@@ -581,17 +581,21 @@
 int i = 
-0
+index
 ; i %3C s.
@@ -630,26 +630,17 @@
  String 
-leftSubstr
+a
  = s.sub
@@ -646,17 +646,21 @@
 bstring(
-0
+index
 , i + 1)
@@ -677,17 +677,16 @@
     if (
-!
 isPalind
@@ -690,26 +690,17 @@
 indrome(
-leftSubstr
+a
 )) %7B%0A   
@@ -716,253 +716,115 @@
     
-continue;%0A            %7D else %7B%0A                List%3CString%3E newAccu = new ArrayList%3C%3E(accu);%0A                newAccu.add(leftSubstr);%0A                String rightSubstr = s.substring(i + 1);%0A                partition(rightSubstr, newAccu, result
+accu.add(a);%0A                partition(s, i + 1, accu, answer);%0A                accu.remove(accu.size() - 1
 );%0A 
@@ -865,16 +865,23 @@
 private 
+static 
 boolean 
@@ -917,125 +917,122 @@
     
-char%5B%5D chars = s.toCharArray();%0A        int left = 0;%0A        int right = s.length() - 1;%0A        while (left %3C right
+if (s.isEmpty()) %7B%0A            return false;%0A        %7D%0A        for (int i = 0, j = s.length() - 1; i %3C j; i++, j--
 ) %7B%0A
@@ -1051,39 +1051,34 @@
 if (
+s.
 char
-s%5Bleft++%5D
+At(i)
  != 
+s.
 char
-s%5Bright--%5D
+At(j)
 ) %7B%0A

Update problem 131