Palindromic Substrings
Description:
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
Input: “abc”
Output: 3
Explanation: Three palindromic strings: “a”, “b”, “c”.
Code:
class Solution:
"""
@param str: s string
@return: return an integer, denote the number of the palindromic substrings
"""
def countPalindromicSubstrings(self, str):
# write your code here
cnt = 0
ls = len(str)
for i in range(ls):
cnt += 1
l = i-1
r = i+1
if l>=0 and r<ls and str[l]==str[r]:
cnt += 1
l -= 1
r += 1
l = i-1
r = i
if l>=0 and r<ls and str[l]==str[r]:
cnt += 1
l -= 1
r += 1
return cnt