Check if a string is Colindrome in Python

PythonServer Side ProgrammingProgramming

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
  • return True

Let us see the following implementation to get better understanding −

Example

 Live Demo

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
raja
Published on 29-Dec-2020 17:50:38
Advertisements