Sudo Placement | Palindrome Family

Given a string of lowercase characters, the task is to detect the family of string, where family of string is described as follows.

  • ODD Palindrome : String with characters at odd index (1-based indexing) forming Palindrome.
  • EVEN Palindrome : String with characters at even index (1-based indexing) forming Palindrome.
  • TWIN Palindrome : String with both of the above properties.
  • PARENT Palindrome : If the string is itself a Palindrome.

Examples:

Input : geeksforskeeg
Output : ODD Palindrome
Explanation: The string with characters at odd indices(following 1-based indexing) is ‘gesoseg‘, which is a palindrome, while the string formed by characters at even indices does not form a palindrome. Thus the given string is of ‘ODD‘ Family.



Input : aibohobia
Output : PARENT Palindrome
Explanation: The string itself is a palindrome, thus falls under PARENT Family.

Approach: Define 2 empty strings, oddString and evenString.

  • Append all the characters at even indices in the evenString.
  • Append all the characters at odd indices in the oddString.

Now, check for the following cases:

  1. Check if the given string is a Palindrome, if it is Print ‘PARENT Palindrome’.
  2. If the first case is not true, check if both evenString and oddString are palindromes, if so then print ‘TWIN Palindrome’
  3. If the second case doesn’t hold true, then if evenString is a Palindrome, print ‘EVEN Palindrome’, else if oddString is a Palindrome print ‘ODD Palindrome’.
  4. If none of the above conditions satisfy, print ‘ALIEN Palindrome’.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print the Palindrome Family
// corresponding to a given string
#include <bits/stdc++.h>
  
using namespace std;
  
// Checks if the given string is a Palindrome
bool isPalindrome(string str)
{
    // Find the reverse of the given string
    string reverse_str = str;
    reverse(reverse_str.begin(), reverse_str.end());
  
    // Check if the reverse and the string are equal
    if (str == reverse_str)
        return true;
    return false;
}
  
// Prints the Palindrome Family corresponding to a given string
void printPalindromeFamily(string str)
{
  
    // Check if the given string is a palindrome
    if (isPalindrome(str)) {
        cout << "PARENT Palindrome" << endl;
        return;
    }
  
    string oddString = "";
    string evenString = "";
  
    int n = str.length();
  
    // append characters at odd indices(1 based) to oddString
    for (int i = 0; i < n; i += 2)
        oddString += str[i];
  
    // append characters at even indices(1 based indexing) to evenString
    for (int i = 1; i < n; i += 2)
        evenString += str[i];
  
    // Check if the individual evenString and oddString are palindrome
    bool isEvenPalindrome = isPalindrome(evenString);
    bool isOddPalindrome = isPalindrome(oddString);
  
    // Check if both oddString and evenString are palindromes
    // If so, it is a TWIN palindrome
    if (isEvenPalindrome && isOddPalindrome)
        cout << "TWIN Palindrome" << endl;
  
    // Else check if even indices form a palindrome
    else if (isEvenPalindrome)
        cout << "EVEN Palindrome" << endl;
  
    // Else check if odd indices form a palindrome
    else if (isOddPalindrome)
        cout << "ODD Palindrome" << endl;
  
    // If none of the cases satisfy, then it is an ALIEN Palindrome
    else
        cout << "Alien Palindrome" << endl;
}
  
// Driver Code
int main()
{
  
    string s = "geeksforskeeg";
    printPalindromeFamily(s);
  
    s = "aibohobia";
    printPalindromeFamily(s);
  
    s = "geeks";
    printPalindromeFamily(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the Palindrome Family
// corresponding to a given string
import java.util.*;
import java.io.*;
  
public class PalindromeFamily {
  
    // Checks if the given string is a Palindrome
    public static boolean isPalindrome(String str){
  
        //Set two pointers, one at the last character of the string and
        // other the first character. If both of them don't match, then
        // it is not a palindrome. Keep incrementing start pointer,
        // and decreasing end pointer by one, until they check the middle character.
        int start = 0, end = str.length() -1;
        while(start <= end){
            if(str.charAt(start) != str.charAt(end)){
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
  
    // Prints the Palindrome Family corresponding to a given string
    public static void palindromeFamily(String str){
          
        //Check for parent palindrome
        if(isPalindrome(str)){
            System.out.println("PARENT Palindrome");
            return;
        }
  
        //Check for odd and even palindromes
        String oddString = "";
        String evenString = "";
  
        // append characters at odd indices(1 based) to oddString
        for(int i=0; i<str.length(); i+=2){
            oddString += str.charAt(i);
        }
  
        // append characters at even indices(1 based indexing) to evenString
        for(int i=1; i<str.length(); i+=2){
            evenString += str.charAt(i);
        }
  
         // Check if the individual evenString and oddString are palindrome
        boolean isEvenPalindrome = isPalindrome(evenString);
        boolean isOddPalindrome = isPalindrome(oddString);
  
        //Check if both oddString and evenString are palindromes
        //If yes, then it is a twin palindrome
        if(isOddPalindrome && isEvenPalindrome){
            System.out.println("TWIN Palindrome");
        }
        //Else, check if odd indices form a palindrome
        else if (isOddPalindrome){
            System.out.println("ODD Palindrome");
        }
        //Else, check if even indices form a palindrome
        else if (isEvenPalindrome){
            System.out.println("EVEN Palindrome");
        }
        //If none of the cases satisfy, then it is an ALIEN palindrome
        else
            System.out.println("ALIEN Palindrome");
  
    }
  
    public static void main(String[] args){
        String s = "geeksforskeeg";
        palindromeFamily(s);
  
        s = "aibohobia";
        palindromeFamily(s);
  
        s = "geeks";
        palindromeFamily(s);
    }
  
}

chevron_right


Output:

ODD Palindrome
PARENT Palindrome
Alien Palindrome


My Personal Notes arrow_drop_up

This article is contributed by Team GeeksforGeeks. 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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.