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++
// C++ 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 presentbool 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 presentstatic 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 presentdef check(s): for i in range (0, len(s)): if (s[i] == s[i + 1]): return True return False# Driver Codes = "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 presentstatic 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 presentfunction 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?> |
Javascript
<script>// Javascript program to check if there is// a substring palindrome of even length.// Function to check if two consecutive same// characters are presentfunction check(s){ for(let i = 0; i < s.length - 1; i++) if (s[i] == s[i + 1]) return true; return false;}// Driver codelet s = "xzyyz";if (check(s)) document.write("YES");else document.write("NO"); // This code is contributed by suresh07</script> |
Output:
YES



