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++
// 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 ?> |
YES
Recommended Posts:
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Maximum length palindromic substring such that it starts and ends with given char
- Check if all the palindromic sub-strings are of odd length
- Number of strings of length N with no palindromic sub string
- Find length of longest subsequence of one string which is substring of another string
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Convert to a string that is repetition of a substring of k length
- Maximum length substring with highest frequency in a string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Queries to check if string B exists as substring in string A
- Check if a string is substring of another
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Python | Check if a Substring is Present in a Given String
- Check if string can be made lexicographically smaller by reversing any substring
- Make palindromic string non-palindromic by rearranging its letters
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.


