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:
- Check if the given string is a Palindrome, if it is Print ‘PARENT Palindrome’.
- If the first case is not true, check if both evenString and oddString are palindromes, if so then print ‘TWIN Palindrome’
- 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’.
- If none of the above conditions satisfy, print ‘ALIEN Palindrome’.
Below is the implementation of above approach:
C++
// 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; } |
Java
// 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); } } |
Python3
# Python3 Program to print the Palindrome Family # corresponding to a given string # check if the given string is a Palindrome def isPalindrome(str1): # Find the reverse of the given string reverse_str = str1[:: - 1 ] # Check if the reverse and the string are equal if (str1 = = reverse_str): return True return False # Prints the palindrome family corresponding to a given string def printPalindromeFamily(str1): # Check if the given string is a palindrome if (isPalindrome(str1)): print ( "PARENT Palindrome" ) return False oddString = "" evenString = "" n = len (str1) # append characters at odd # indices(1 based) to oddString for i in range ( 0 , n, 2 ): oddString + = str1[i] # append characters at even # indices(1 based) to evenString for i in range ( 1 , n, 2 ): evenString + = str1[i] # check if the individual evenString and # OddString are palindromes isEvenPalindrome = isPalindrome(evenString) isOddPalindrome = isPalindrome(oddString) # Check if both oddString and evenString are palindromes # If so, it is a twin palindrome if (isEvenPalindrome and isOddPalindrome): print ( "TWIN Palindrome" ) elif (isEvenPalindrome): print ( "EVEN Palindrome" ) elif (isOddPalindrome): print ( "ODD Palindrome" ) else : print ( "Alien Palindrome" ) # Driver code s = "geeksforskeeg" printPalindromeFamily(s) s = "aibohobia" printPalindromeFamily(s) s = "geeks" printPalindromeFamily(s) # This code is contributed by simranjenny84 |
ODD Palindrome PARENT Palindrome Alien Palindrome