- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find all palindromic sub-strings of a given string - Set 2 in Python
Suppose we have a string; we have to find all the palindromic sub-strings from that string. Here aa and aa are considered as two sub-strings, not one.
So, if the input is like redivider, then the output will be ['r', 'e', 'd', 'i', 'v', 'ivi', 'divid', 'edivide', 'redivider', 'i', 'd', 'e', 'r']
To solve this, we will follow these steps −
- v := a new list
- pos := 0.0
- while pos < size of s, do
- rad := pos - (pos as integer)
- while (pos + rad) < size of s and (pos - rad) >= 0 and (s[integer of (pos - rad)] is same as s[integer of (pos + rad)]), do
- insert s[from index integer of (pos - rad) to integer of (pos + rad + 1)] at the end of v
- rad := rad + 1
- pos := pos + 0.5
- return v
Example Code
Let us see the following implementation to get better understanding −
def get_all_pal_sub(s): v = [] pos = 0.0 while pos < len(s): rad = pos - int(pos) while ((pos + rad) < len(s) and (pos - rad) >= 0 and (s[int(pos - rad)] == s[int(pos + rad)])): v.append(s[int(pos - rad): int(pos + rad + 1)]) rad += 1 pos += 0.5 return v v = get_all_pal_sub("redivider") print(len(v)) print(v)
Input
"redivider"
Output
13 ['r', 'e', 'd', 'i', 'v', 'ivi', 'divid', 'edivide', 'redivider', 'i', 'd', 'e', 'r']
- Related Articles
- Find all distinct palindromic sub-strings of a given String in Python
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Check if all the palindromic sub-strings are of odd lengths in Python
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Check if a string contains a palindromic sub-string of even length in Python
- Count all Palindrome Sub-Strings in a String in C++
- Count all Palindromic Subsequence in a given String in C++
- Check if a string contains a palindromic sub-string of even length in C++
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all palindromic partitions of a string in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Count of sub-strings of length n possible from the given string in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Python - Find all the strings that are substrings to the given list of strings

Advertisements