
- 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
Check if all the palindromic sub-strings are of odd lengths in Python
Suppose we have a string s we have to check whether its palindromic sub-strings are of odd length or not.
So, if the input is like s = "levelopmadam", then the output will be True as there are two palindromic substrings "level" and "madam" both are of odd length.
To solve this, we will follow these steps −
- for i in range 0 to size of s, do
- temp := blank string
- for j in range i to size of s, do
- temp := temp concatenate s[j]
- if size of temp is even and temp is palindrome, then
- return False
- return True
Let us see the following implementation to get better understanding −
Example
def is_palindrome(s): return s == s[::-1] def solve(s): for i in range(len(s)): temp = "" for j in range(i, len(s)): temp += s[j] if len(temp) % 2 == 0 and is_palindrome(temp): return False return True s = "levelopmadam" print(solve(s))
Input
"levelopmadam"
Output
True
- 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
- Program to check whether all palindromic substrings are of odd length or not in Python
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Check if all sub-numbers have distinct Digit product in Python
- Python - Check if two strings are isomorphic in nature
- Check if strings are rotations of each other or not in Python
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Check if all array elements are distinct in Python
- Check if all bits of a number are set in Python
- Python - Check If All the Characters in a String Are Alphanumeric?
- Check if all elements of the array are palindrome or not in Python
- Check if reversing a sub array make the array sorted in Python

Advertisements