Given a string, your task is to count how many palindromic substrings in this string.



The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. 


Example 1:

Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".



Example 2:

Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".



Note:

The input string length won't exceed 1000.

Difficulty: interview

Starter code:

class Solution:
    def countSubstrings(self, s: str) -> int:
        

Solutions:
class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         ret = 0
         left, right = 0, 0
         while left < len(s):
             while right < len(s) and s[right] == s[left]:
                 right += 1
             ret += self.sum(right - left)
             l, r = left-1, right
             while l >= 0 and r < len(s) and s[r] == s[l]:
                 ret += 1
                 l -= 1
                 r += 1
             left = right
         return ret
 
     def sum(self, n):
         s = 0
         for i in range(1, n + 1):
             s += i
         return s

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         
         # manacher, find Z
         s = '^#' + '#'.join(s) + '#$'
         Z = [1] * len(s)
         center, right = 0, 0
         for i in range(len(s)-1): # 注意 i不能取到 $
             if i < right:
                 i_mirror = 2 * center - i
                 Z[i] = min(Z[i_mirror], right - i + 1)
             while s[i+Z[i]] == s[i-Z[i]]:
                 Z[i] += 1
             if i + Z[i] - 1 > right:
                 center, right = i, i + Z[i] - 1
         
         # sum Z
         return sum(z//2 for z in Z)

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         def manachers(S):
             A = '@#' + '#'.join(S) + '#$'
             Z = [0] * len(A)
             center = right = 0
             for i in range(1, len(A) - 1):
                 if i < right:
                     Z[i] = min(right - i, Z[2 * center - i])
                 while A[i + Z[i] + 1] == A[i - Z[i] - 1]:
                     Z[i] += 1
                 if i + Z[i] > right:
                     center, right = i, i + Z[i]
             return Z
         return sum((v + 1) // 2 for v in manachers(s))   

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         t = '@#' + '#'.join(s) + '#$'
         n = len(t)
         r = [0] * n
         c = 0
         for i in range(1, n-1):
             if i-c < r[c]:
                 if r[c-(i-c)] < r[c]-(i-c):
                     r[i] = r[c-(i-c)]
                 else:
                     r[i] = r[c]-(i-c)
                     while t[i-r[i]-1] == t[i+r[i]+1]:
                         r[i] += 1
                     c = i
             else:
                 r[i] = 0
                 while t[i-r[i]-1] == t[i+r[i]+1]:
                     r[i] += 1
                 c = i
         return sum((i+1) // 2 for i in r)
                 


class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         ss = "$#"
         for i in s:
             ss += i + '#'
         ss += '@'
         r = [0] * (len(ss) + 1) 
         pos = 0
         ma = 0
         for i in range(1, len(ss) - 1):
             if ma > i:
                 r[i] = min(r[pos - (i - pos)], ma - i)
             else:
                 r[i] = 1
             while ss[i + r[i]] == ss[i - r[i]]:
                 r[i] += 1
             # print(i, r[i])
             if r[i] + i > ma:
                 pos, ma = i, r[i] + i
         ans = sum(int(l/2) for l in r)
         return ans

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         
         # manacher, find Z
         s = '^#' + '#'.join(s) + '#$'
         Z = [1] * len(s)
         center, right = 0, 0
         for i in range(len(s)-1): # 注意 i不能取到 $
             if i < right:
                 i_mirror = 2 * center - i
                 Z[i] = min(Z[i_mirror], right - i + 1)
             while s[i+Z[i]] == s[i-Z[i]]:
                 Z[i] += 1
             if i + Z[i] - 1 > right:
                 center, right = i, i + Z[i] - 1
         
         # sum Z
         return sum(z//2 for z in Z)

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         
         s = '^#' + '#'.join(s) + '#$'
         Z = [1] * len(s)
         center, right = 0, 0
         for i in range(len(s)-1):
             if i < right:
                 i_mirror = 2*center - i
                 Z[i] = min(Z[i_mirror], right-i+1)
             while s[i+Z[i]] == s[i-Z[i]]:
                 Z[i] += 1
             if i+Z[i]-1 > right:
                 center, right = i, i+Z[i]-1
         return sum(z//2 for z in Z)

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         
         # manacher
         s = '^#' + '#'.join(s) + '#$'
         Z = [1] * len(s)
         center, right = 0, 0
         for i in range(len(s)-1):
             if i < right:
                 i_mirror = 2*center - i
                 Z[i] = min(Z[i_mirror], right-i+1)
             while s[i+Z[i]] == s[i-Z[i]]:
                 Z[i] += 1
             if i+Z[i]-1 > right:
                 center, right = i, i+Z[i]-1
         return sum(z//2 for z in Z)

class Solution:
     def countSubstrings(self, s):
         """
         :type s: str
         :rtype: int
         """
         s=','+s+'.'
         ans=0
         for i in range(1,len(s)-1):
             j=1
             while s[i+j]==s[i-j]:
                 j+=1
             ans+=j
         for i in range(1,len(s)-1):
             j=0
             while s[i-j]==s[i+1+j]:
                 j+=1
             ans+=j
         return ans