- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Check if a string is Colindrome in Python
Suppose we have a string s. We have to check whether the given string is colindrome or not. The colindrome is a concatenated string of 6 length palindromes.
So, if the input is like s = "aabbaamnoonm", then the output will be True as this contains palindromes like "aabbaa" and "mnoonm", both are of length 6.
To solve this, we will follow these steps −
- if size of s is not multiple of 6, then
- return False
- for i in range 0 to size of s - 1, increase by 6, do
- if s[from index i to i+5] is not palindrome, then
- return False
- if s[from index i to i+5] is not palindrome, then
- return True
Let us see the following implementation to get better understanding −
Example
def is_palindrome(s): return s == s[::-1] def solve(s): if len(s) % 6 != 0: return False for i in range(0, len(s), 6): if not is_palindrome(s[i : i+6]): return False return True s = "aabbaamnoonm" print(solve(s))
Input
"aabbaamnoonm"
Output
True
Advertisements