Dada una string str , la tarea es verificar si la string dada es Palindrome par-impar o no.
Una string de palíndromo par-impar se define como una string cuyos caracteres en índices pares forman un palíndromo mientras que los caracteres en índices impares también forman un palíndromo por separado.
Ejemplos:
Entrada: str=”abzzab”
Salida: SI
Explicación:
String formada por caracteres en índices impares: bzb , que es un Palíndromo.
String formada por caracteres en índices pares: aza , que es un palíndromo.
Por lo tanto, la string dada es un palíndromo par-impar.
Entrada: str=”daccad”
Salida: NO
Enfoque: para resolver el problema, cree una nueva string agregando los caracteres impares indexados de la string dada y verifique si las strings formadas son palindrómicas o no. Del mismo modo, verifique los caracteres indexados uniformes . Si ambas strings son palindrómicas, imprima «Sí» . De lo contrario, escriba “No” .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the string // str is palindromic or not bool 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 indices string 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 indices string 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 not 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)) cout << "Yes" << endl; else cout << "No" << endl; } // Driver Code int main() { string str = "abzzab"; checkevenOddPalindrome(str); return 0; }
Java
// Java program implementation // of the approach import java.util.*; import java.io.*; class GFG{ // Function to check if the string // str is palindromic or not static 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 indices static 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 indices static 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 not static 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 code public static void main(String[] args) { String str = "abzzab"; checkevenOddPalindrome(str); } } // This code is contributed by sanjoy_62
Python3
# Python3 program to implement # the above approach # Function to check if the string # str is palindromic or not def 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 indices def makeOddString(Str): odd = "" for i in range(1, len(Str), 2): odd += Str[i] return odd # Function to generate string # from characters at even indices def 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 not def 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 code Str = "abzzab" checkevenOddPalindrome(Str) # This code is contributed by himanshu77
C#
// C# program implementation // of the approach using System; class GFG{ // Function to check if the string // str is palindromic or not static 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 indices static 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 indices static 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 not static 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 code public static void Main() { string str = "abzzab"; checkevenOddPalindrome(str); } } // This code is contributed by sanjoy_62
Javascript
<script> // Javascript program implementation // of the approach // Function to check if the string // str is palindromic or not function 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 indices function 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 indices function 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 not function 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 code var str = "abzzab"; checkevenOddPalindrome(str); // This code is contributed by Khushboogoyal499 </script>
Yes
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(n)
Publicación traducida automáticamente
Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA