Dada una string de caracteres en minúsculas, la tarea es detectar la familia de strings, donde la familia de strings se describe a continuación.
- ODD Palindrome : string con caracteres en índice impar (indexación basada en 1) que forma Palindrome.
- EVEN Palindrome : string con caracteres en el índice par (indexación basada en 1) que forma Palindrome.
- TWIN Palindrome : string con las dos propiedades anteriores.
- PADRE Palíndromo : si la string es en sí misma un palíndromo.
Ejemplos :
Entrada : geeksforskeeg
Salida : ODD Palindrome
Explicación : la string con caracteres en índices impares (siguiendo la indexación basada en 1) es ‘ gesoseg ‘, que es un palíndromo, mientras que la string formada por caracteres en índices pares no forma un palíndromo. Por lo tanto, la string dada es de la familia ‘ IMPAR ‘.
Entrada : aibohobia
Salida : PADRE Palíndromo
Explicación : la string en sí es un palíndromo, por lo tanto, pertenece a la familia PARENT .
Enfoque : defina 2 strings vacías, oddString y evenString.
- Agregue todos los caracteres en índices pares en evenString.
- Agregue todos los caracteres en índices impares en oddString.
Ahora, verifique los siguientes casos:
- Verifique si la string dada es un Palindrome, si es Print ‘PARENT Palindrome’.
- Si el primer caso no es cierto, verifique si tanto la string par como la string impar son palíndromos, si es así, imprima ‘TWIN Palindrome’
- Si el segundo caso no es cierto, entonces si evenString es un Palindrome, imprima ‘EVEN Palindrome’, de lo contrario, si oddString es un Palindrome, imprima ‘ODD Palindrome’.
- Si no cumple ninguna de las condiciones anteriores, imprima ‘ALIEN Palindrome’.
A continuación se muestra la implementación del enfoque anterior:
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
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA