<p>Given a string <code>s</code>, return <em>the number of <strong>palindromic substrings</strong> in it</em>.</p>

<p>A string is a <strong>palindrome</strong> when it reads the same backward as forward.</p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within the string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Three palindromic strings: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aaa&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> Six palindromic strings: &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;aa&quot;, &quot;aa&quot;, &quot;aaa&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

import numpy as np
class Solution:
    def is_palindrome(self, s: str) -> bool:
        if len(s) == 1:
            return True
        sub_string = s[:(len(s) )//2]

        end_string = s[-(len(s)//2):][::-1]

        return sub_string == end_string
    
    def count_subpalindromes(self, s:str) -> int:
        if len(s) == 1:
            return 1
        return 2+sum([int(self.is_palindrome(s[idx:])) for idx in range(1,len(s)-1)])

    def countSubstrings(self, s: str) -> str:
        if len(s) == 1:
            return 1
        # Create a list of the longest palindromes ending at index j
        longest_palindrome_indices = [[0, 0]]*(len(s))

        #Dynamically determine the longest palindrome
        longest_palindrome_indices[0] = [0,1]

        n_palindrome_count = 1
        for idx in range(1, len(s)):
            new_char_idx = longest_palindrome_indices[idx-1][0]-1
            print(idx, new_char_idx)
            if new_char_idx >= 0:
                # Longest palindrome adds two characters -- adding immediate predecessor and new
                if s[idx] == s[new_char_idx]:
                    longest_palindrome_indices[idx] = [new_char_idx,idx+1]
                else:
                    next_char = new_char_idx + 1
                    while not self.is_palindrome(s[next_char:idx+1]):
                        next_char += 1
                    longest_palindrome_indices[idx] = [next_char,idx+1]

            else:
                next_char = new_char_idx + 1
                while not self.is_palindrome(s[next_char:idx+1]):
                    next_char += 1
                longest_palindrome_indices[idx] = [next_char,idx+1]
            
            longest_palindrome = s[longest_palindrome_indices[idx][0]:idx+1]

            n_palindrome_count += self.count_subpalindromes(longest_palindrome)
        return n_palindrome_count


            
            


29505