package leetcode;

/**
 * https://leetcode.com/problems/palindromic-substrings/
 */
public class Problem647 {
    public int countSubstrings(String s) {
        return countSubstrings(s, 0, s.length(), new boolean[s.length() + 1][s.length() + 1],
            new Integer[s.length() + 1][s.length() + 1]);
    }

    private static int countSubstrings(String s, int i, int j, boolean[][] processed, Integer memo[][]) {
        if (i >= j) {
            return 0;
        }
        String sub = s.substring(i, j);
        int count = 0;
        if (processed[i][j]) {
            return 0;
        }
//        if (memo[i][j] != null) {
//            return memo[i][j];
//        }
        if (isPalindrome(sub)) {
            processed[i][j] = true;
            count++;
        }
        int a = countSubstrings(s, i + 1, j, processed, memo);
        int b = countSubstrings(s, i, j - 1, processed, memo);
        int c = countSubstrings(s, i + 1, j - 1, processed, memo);
        count += a + b + c;
//        memo[i][j] = count;
        return count;
    }

    private static boolean isPalindrome(String s) {
        for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Problem647 prob = new Problem647();
//        System.out.println(prob.countSubstrings("abc")); // 3
//        System.out.println(prob.countSubstrings("abbc")); // 5
//        System.out.println(prob.countSubstrings("aaa")); // 6
        System.out.println(prob.countSubstrings("aaabbbccc")); // 18
        System.out.println(prob.countSubstrings("aabaa")); // 9
//        System.out.println(prob.countSubstrings("xkjkqlajprjwefilxgpdpebieswu")); // 30
    }
}

@@ -239,65 +239,8 @@
 () +
- 1%5D,%0A            new Integer%5Bs.length() + 1%5D%5Bs.length() +
  1%5D)
@@ -336,26 +336,8 @@
 ssed
-, Integer memo%5B%5D%5B%5D
 ) %7B%0A
@@ -394,71 +394,8 @@
   %7D%0A
-        String sub = s.substring(i, j);%0A        int count = 0;%0A
     
@@ -453,26 +453,24 @@
       %7D%0A
-//
         
 if (memo
@@ -465,87 +465,71 @@
     
-if (memo%5Bi%5D%5Bj%5D != null) %7B%0A//            return memo%5Bi%5D%5Bj%5D;%0A//        %7D%0A
+String sub = s.substring(i, j);%0A        int count = 0;%0A   
      
-   
 if (
@@ -553,44 +553,8 @@
 ) %7B%0A
-            processed%5Bi%5D%5Bj%5D = true;%0A
     
@@ -626,38 +626,32 @@
  1, j, processed
-, memo
 );%0A        int b
@@ -695,81 +695,8 @@
 ssed
-, memo);%0A        int c = countSubstrings(s, i + 1, j - 1, processed, memo
 );%0A 
@@ -720,28 +720,27 @@
  + b
- + c;%0A//        memo
+;%0A        processed
 %5Bi%5D%5B
@@ -744,21 +744,20 @@
 i%5D%5Bj%5D = 
-count
+true
 ;%0A      
@@ -1025,519 +1025,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem647 prob = new Problem647();%0A//        System.out.println(prob.countSubstrings(%22abc%22)); // 3%0A//        System.out.println(prob.countSubstrings(%22abbc%22)); // 5%0A//        System.out.println(prob.countSubstrings(%22aaa%22)); // 6%0A        System.out.println(prob.countSubstrings(%22aaabbbccc%22)); // 18%0A        System.out.println(prob.countSubstrings(%22aabaa%22)); // 9%0A//        System.out.println(prob.countSubstrings(%22xkjkqlajprjwefilxgpdpebieswu%22)); // 30%0A    %7D
 %0A%7D%0A

Solve problem 647