Skip to content
Related Articles

Related Articles

Check if a string contains a palindromic sub-string of even length
  • Difficulty Level : Easy
  • Last Updated : 07 Jan, 2019

S is string containing only lowercase English alphabets. We need to find if there exists at least one palindromic sub-string whose length is even.

Examples:

Input  : aassss
Output : YES

Input  : gfg
Output : NO

Notice that a palindrome of even length must contain two same alphabets in the middle. So we just need to check for this condition. If we find two consecutive same alphabets in the string then we output “YES” otherwise “NO”.

Below is the implementation:

C++




// CPP program to check if there is a substring
// palindrome of even length.
#include <bits/stdc++.h>
using namespace std;
  
// function to check if two consecutive same 
// characters are present
bool check(string s)
{
    for (int i = 0; i < s.length() - 1; i++)
        if (s[i] == s[i + 1])
            return true;
    return false;
}
  
int main()
{
    string s = "xzyyz";
    if (check(s))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

Java




// Java program to check if there is a substring 
// palindrome of even length. 
  
class GFG {
  
  
// function to check if two consecutive same 
// characters are present 
static boolean check(String s) 
    for (int i = 0; i < s.length() - 1; i++) 
        if (s.charAt(i) == s.charAt(i+1)) 
            return true
    return false
  
// Driver Code 
    public static void main(String[] args) {
  
        String s = "xzyyz"
    if (check(s)) 
              System.out.println("YES");
    else
        System.out.println("NO");
    }
}

Python3




# Python 3 program to check if there is 
# a substring palindrome of even length.
  
# function to check if two consecutive
# same characters are present
def check(s):
  
    for i in range (0, len(s)):
        if (s[i] == s[i + 1]):
            return True
              
    return False
  
# Driver Code
s = "xzyyz"
if(check(s)):
    print("YES")
else:
    print("NO")
      
# This code is contributed
# by iAyushRAJ

C#




// C# program to check if there is a substring 
// palindrome of even length. 
using System; 
public class GFG {
  
  
// function to check if two consecutive same 
// characters are present 
static bool check(String s) 
    for (int i = 0; i < s.Length - 1; i++) 
        if (s[i] == s[i+1]) 
            return true
    return false
  
// Driver Code 
    public static void Main() {
  
        String s = "xzyyz"
    if (check(s)) 
              Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
    }
}

PHP




<?php
// PHP program to check if there is a 
// substring palindrome of even length. 
  
// function to check if two consecutive 
// same characters are present 
function check($s
    for ($i = 0; $i < strlen($s) - 1; $i++) 
        if ($s[$i] == $s[$i + 1]) 
            return true; 
    return false; 
  
// Driver Code
$s = "xzyyz"
if (check($s)) 
    echo "YES","\n"
else
    echo "NO" ,"\n"
  
// This code is contributed by ajit
?>
Output:
YES

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA




My Personal Notes arrow_drop_up
Recommended Articles
Page :