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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, Rajput-Ji, iAyushRaj