
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the count of palindromic sub-string of a string in its sorted form in Python
Suppose we have a string of lowercase characters (all are ASCII characters), we have to find all distinct continuous palindromic sub-strings of the given string.
So, if the input is like "level", then the output will be 7 as there are seven substrings ['level', 'eve', 'l', 'e', 'v', 'e', 'l'].
To solve this, we will follow these steps −
N := 26
n := length of str
sum := 0
my_map := a list of size N and fill with 0
for i in range 0 to n, do
my_map[ASCII of (str[i]) - ASCII of ('a') ] := my_map[ASCII of (str[i]) - ASCII of ('a') ] + 1
for i in range 0 to N, do
if my_map[i] is non-zero, then
sum := sum +(my_map[i] *(my_map[i] + 1) / 2)
return sum
Example
Let us see the following implementation to get better understanding −
N = 26 def all_palindrome_substr_count(str): n = len (str) sum = 0 my_map = [0] * N for i in range(n): my_map[ord(str[i]) - ord('a')] += 1 for i in range(N) : if (my_map[i]): sum += (my_map[i] * (my_map[i] + 1) // 2) return sum str = "level" print (all_palindrome_substr_count(str))
Input
"level"
Output
7
- Related Articles
- Find all distinct palindromic sub-strings of a given String in Python
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Check if a string contains a palindromic sub-string of even length in Python
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Check if a string contains a palindromic sub-string of even length in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Count all Palindromic Subsequence in a given String in C++
- Program to find out the palindromic borders in a string in python
- Program to find lexicographically smallest non-palindromic string in Python
- How to replace a sub-string with the reverse of that sub-string in R?
- Count all Palindrome Sub-Strings in a String in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Count of sub-strings of length n possible from the given string in C++
- Print all palindromic partitions of a string in C++
- Queries to find the last non-repeating character in the sub-string of a given string in C++

Advertisements