Palindromic substrings¶

1) Increment on the letters of the string¶
2) For each letter, find all palindrome having this letter has center (and center left)¶
3) Add this number to the count¶
4) Return the count¶
In [ ]:
def countSubstrings(s: str) -> int:
    count_palindrome = 0
    
    # i is the index of the current center of the tested palindroms 
    for i in range(len(s)):
        count_palindrome += 1 # s[i] is a palindrome

        # Palindromes with odd number of caracters (center is s[i])
        j = 1
        while (i - j >= 0) and (i + j < len(s)) and (s[i - j] == s[i + j]):
            count_palindrome += 1
            j += 1

        # Palindromes with even number of caracters (center between s[i] and s[i])
        j = 1
        while (i - j + 1 >= 0) and (i + j < len(s)) and (s[i - j + 1] == s[i + j]):
            count_palindrome += 1
            j += 1
            
    return(count_palindrome)