Check if a given string is Even-Odd Palindrome or not - GeeksforGeeks

07 Apr, 2021
Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. 
An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. 
Examples:  
Input: str=”abzzab” Output:YES Explanation: String formed by characters at odd indices: bzb, which is a Palindrome. String formed by characters at even indices: aza, which is a Palindrome. Hence, the given string is an Even-Odd Palindrome.Input: str=”daccad” Output: NO  
Approach: To solve the problem, create a new String by appending the Odd Indexed Characters of the given string and check if the strings formed are palindromic or not. Similarly, check for Even Indexed Characters. If both the strings are palindromic, then print “Yes”. Otherwise, print “No”.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to check if the string// str is palindromic or notbool isPalindrome(string str){     // Pointers to iterate the    // string from both ends    int l = 0;    int h = str.size() - 1;     while (h > l) {         // If characters are found        // to be distinct        if (str[l++] != str[h--]) {            return false;        }    }     // Return true if the    // string is palindromic    return true;} // Function to generate string// from characters at odd indicesstring makeOddString(string str){    string odd = "";    for (int i = 1; i < str.size();         i += 2) {        odd += str[i];    }     return odd;} // Function to generate string// from characters at even indicesstring makeevenString(string str){    string even = "";    for (int i = 0; i < str.size();         i += 2) {        even += str[i];    }     return even;} // Functions to checks if string// is Even-Odd Palindrome or notvoid checkevenOddPalindrome(string str){     // Generate odd indexed string    string odd = makeOddString(str);     // Generate even indexed string    string even = makeevenString(str);     // Check for Palindrome    if (isPalindrome(odd)        && isPalindrome(even))        cout << "Yes" << endl;    else        cout << "No" << endl;} // Driver Codeint main(){    string str = "abzzab";     checkevenOddPalindrome(str);    return 0;}
// Java program implementation// of the approachimport java.util.*;import java.io.*; class GFG{ // Function to check if the string// str is palindromic or notstatic boolean isPalindrome(String str){     // Pointers to iterate the    // string from both ends    int l = 0;    int h = str.length() - 1;     while (h > l)    {                 // If characters are found        // to be distinct        if (str.charAt(l++) !=            str.charAt(h--))            return false;    }     // Return true if the    // string is palindromic    return true;} // Function to generate string// from characters at odd indicesstatic String makeOddString(String str){    String odd = "";         for(int i = 1; i < str.length(); i += 2)    {        odd += str.charAt(i);    }     return odd;} // Function to generate string// from characters at even indicesstatic String makeevenString(String str){    String even = "";         for(int i = 0; i < str.length(); i += 2)    {        even += str.charAt(i);    }     return even;} // Functions to checks if string// is Even-Odd Palindrome or notstatic void checkevenOddPalindrome(String str){     // Generate odd indexed string    String odd = makeOddString(str);     // Generate even indexed string    String even = makeevenString(str);     // Check for Palindrome    if (isPalindrome(odd) && isPalindrome(even))        System.out.println("Yes");    else        System.out.println("No");} // Driver codepublic static void main(String[] args){    String str = "abzzab";     checkevenOddPalindrome(str);}} // This code is contributed by sanjoy_62
# Python3 program to implement# the above approach # Function to check if the string# str is palindromic or notdef isPalindrome(Str):     # Pointers to iterate the    # string from both ends    l = 0    h = len(Str) - 1     while (h > l):         # If characters are found        # to be distinct        if (Str[l] != Str[h]):            return False         l += 1        h -= 1     # Return true if the    # string is palindromic    return True # Function to generate string# from characters at odd indicesdef makeOddString(Str):     odd = ""    for i in range(1, len(Str), 2):        odd += Str[i]     return odd # Function to generate string# from characters at even indicesdef makeevenString(Str):     even = ""    for i in range(0, len(Str), 2):        even += Str[i]     return even # Functions to checks if string# is Even-Odd Palindrome or notdef checkevenOddPalindrome(Str):     # Generate odd indexed string    odd = makeOddString(Str)     # Generate even indexed string    even = makeevenString(Str)     # Check for Palindrome    if (isPalindrome(odd) and        isPalindrome(even)):        print("Yes")    else:        print("No") # Driver codeStr = "abzzab" checkevenOddPalindrome(Str) # This code is contributed by himanshu77
// C# program implementation// of the approachusing System; class GFG{     // Function to check if the string// str is palindromic or notstatic bool isPalindrome(string str){         // Pointers to iterate the    // string from both ends    int l = 0;    int h = str.Length - 1;     while (h > l)    {        // If characters are found        // to be distinct        if (str[l++] != str[h--])            return false;    }     // Return true if the    // string is palindromic    return true;} // Function to generate string// from characters at odd indicesstatic string makeOddString(string str){    string odd = "";         for(int i = 1; i < str.Length; i += 2)    {        odd += str[i];    }     return odd;} // Function to generate string// from characters at even indicesstatic string makeevenString(string str){    string even = "";         for(int i = 0; i < str.Length; i += 2)    {        even += str[i];    }     return even;} // Functions to checks if string// is Even-Odd Palindrome or notstatic void checkevenOddPalindrome(string str){     // Generate odd indexed string    string odd = makeOddString(str);     // Generate even indexed string    string even = makeevenString(str);     // Check for Palindrome    if (isPalindrome(odd) && isPalindrome(even))        Console.WriteLine("Yes");    else        Console.WriteLine("No");} // Driver codepublic static void Main(){    string str = "abzzab";     checkevenOddPalindrome(str);}} // This code is contributed by sanjoy_62
<script> // Javascript program implementation// of the approach // Function to check if the string// str is palindromic or notfunction isPalindrome(str){         // Pointers to iterate the    // string from both ends    var l = 0;    var h = str.length - 1;     while (h > l)    {                 // If characters are found        // to be distinct        if (str.charAt(l++) !=            str.charAt(h--))            return false;    }         // Return true if the    // string is palindromic    return true;} // Function to generate string// from characters at odd indicesfunction makeOddString(str){    var odd = "";         for(var i = 1; i < str.length; i += 2)    {        odd += str.charAt(i);    }    return odd;} // Function to generate string// from characters at even indicesfunction makeevenString(str){    var even = "";         for(var i = 0; i < str.length; i += 2)    {        even += str.charAt(i);    }     return even;} // Functions to checks if string// is Even-Odd Palindrome or notfunction checkevenOddPalindrome(str){     // Generate odd indexed string    var odd = makeOddString(str);     // Generate even indexed string    var even = makeevenString(str);     // Check for Palindrome    if (isPalindrome(odd) && isPalindrome(even))        document.write("Yes");    else        document.write("No");} // Driver codevar str = "abzzab"; checkevenOddPalindrome(str); // This code is contributed by Khushboogoyal499    </script>
Yes
 
sanjoy_62
himanshu77
khushboogoyal499
palindrome
Competitive Programming
School Programming
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Difference between Backtracking and Branch-N-Bound technique
5 Best Languages for Competitive Programming
Minimum changes required to make all Array elements Prime
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects