Check if a string contains a palindromic sub-string of even length in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s. We have to check whether this string contains some even length palindrome or not.

So, if the input is like s = "afternoon", then the output will be True as "afternoon" has even length palindrome "noon".

To solve this, we will follow these steps:

  • for i in range 0 to size of string - 1, do
    • if string[i] is same as string[i + 1], then
      • return True
  • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

def solve(string):
   for i in range (0, len(string)):
      if (string[i] == string[i + 1]):
         return True
   return False
s = "afternoon"
print(solve(s))

Input

"afternoon"

Output

True
raja
Published on 29-Dec-2020 17:42:42
Advertisements