Dada la string str , la tarea es verificar si los caracteres en los índices impares de str forman una string palíndromo o no. Si no, escriba «No» , de lo contrario, escriba «Sí» .
Ejemplos:
Entrada: str = “osafdfgsg”, N = 9
Salida: Sí
Explicación:
Los caracteres impares indexados son = { s, f, f, s },
por lo que creará una string palindrómica, “sffs”.
Entrada: str = «addwfefwkll», N = 11
Salida: No
Explicación:
Los caracteres impares indexados son = {d, w, e, w, l},
por lo que no generará una string de palíndromo, «dwewl»
Enfoque ingenuo: el enfoque ingenuo es crear una nueva string agregando caracteres impares indexados de la string dada. Luego, simplemente verifique si la cuerda formada es palindrómica o no. Si la string es palindrómica, imprima «Sí» , de lo contrario, imprima «No» .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for 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) { // Iterate the string str from left // and right pointers int l = 0; int h = str.size() - 1; // Keep comparing characters // while they are same while (h > l) { // If they are not same // then return false if (str[l++] != str[h--]) { return false; } } // Return true if the string is // palindromic return true; } // Function to make string using odd // indices of string str string makeOddString(string str) { string odd = ""; for (int i = 1; i < str.size(); i += 2) { odd += str[i]; } return odd; } // Functions checks if characters at // odd index of the string forms // palindrome or not void checkOddlyPalindrome(string str) { // Make odd indexed string string odd = makeOddString(str); // Check for Palindrome if (isPalindrome(odd)) cout << "Yes" << endl; else cout << "No" << endl; } // Driver Code int main() { // Given string string str = "ddwfefwde"; // Function Call checkOddlyPalindrome(str); return 0; }
Java
// Java program for the above approach class GFG{ // Function to check if the String str // is palindromic or not public static boolean isPalindrome(String str) { // Iterate the String str from left // and right pointers int l = 0; int h = str.length() - 1; // Keep comparing characters // while they are same while (h > l) { // If they are not same // then return false if (str.charAt(l) != str.charAt(h)) { return false; } l++; h--; } // Return true if the String is // palindromic return true; } // Function to make String using odd // indices of String str public static String makeOddString(String str) { String odd = ""; for(int i = 1; i < str.length(); i += 2) { odd += str.charAt(i); } return odd; } // Functions checks if characters at // odd index of the String forms // palindrome or not public static void checkOddlyPalindrome(String str) { // Make odd indexed String String odd = makeOddString(str); // Check for Palindrome if (isPalindrome(odd)) System.out.println("Yes"); else System.out.println("No"); } // Driver Code public static void main(String []args) { // Given String String str = "ddwfefwde"; // Function Call checkOddlyPalindrome(str); } } // This code is contributed by grand_master
Python3
# Python3 program for the above approach # Function to check if the string # str is palindromic or not def isPalindrome(str): # Iterate the string str from # left and right pointers l = 0; h = len(str) - 1; # Keep comparing characters # while they are same while (h > l): # If they are not same # then return false if (str[l] != str[h]): return False; l += 1 h -= 1 # Return true if the string is # palindromic return True; # Function to make string using odd # indices of string str def makeOddString(str): odd = ""; for i in range(1, len(str), 2): odd += str[i]; return odd; # Functions checks if characters at # odd index of the string forms # palindrome or not def checkOddlyPalindrome(str): # Make odd indexed string odd = makeOddString(str); # Check for Palindrome if (isPalindrome(odd)): print("Yes") else: print("No") # Driver code # Given string str = "ddwfefwde"; # Function call checkOddlyPalindrome(str); # This code is contributed by grand_master
C#
// C# program for the above approach using System; class GFG{ // Function to check if the String str // is palindromic or not static bool isPalindrome(string str) { // Iterate the String str from left // and right pointers int l = 0; int h = str.Length - 1; // Keep comparing characters // while they are same while (h > l) { // If they are not same // then return false if (str[l] != str[h]) { return false; } l++; h--; } // Return true if the String is // palindromic return true; } // Function to make String using odd // indices of String str static string makeOddString(string str) { string odd = ""; for(int i = 1; i < str.Length; i += 2) { odd += str[i]; } return odd; } // Functions checks if characters at // odd index of the String forms // palindrome or not static void checkOddlyPalindrome(string str) { // Make odd indexed String string odd = makeOddString(str); // Check for Palindrome if (isPalindrome(odd)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } // Driver code static void Main() { // Given String string str = "ddwfefwde"; // Function Call checkOddlyPalindrome(str); } } // This code is contributed by divyeshrabadiya07
Javascript
<script> // Javascript program for the above approach // Function to check if the string str // is palindromic or not function isPalindrome(str) { // Iterate the string str from left // and right pointers var l = 0; var h = str.length - 1; // Keep comparing characters // while they are same while (h > l) { // If they are not same // then return false if (str[l++] != str[h--]) { return false; } } // Return true if the string is // palindromic return true; } // Function to make string using odd // indices of string str function makeOddString(str) { var odd = ""; for (var i = 1; i < str.length; i += 2) { odd += str[i]; } return odd; } // Functions checks if characters at // odd index of the string forms // palindrome or not function checkOddlyPalindrome(str) { // Make odd indexed string var odd = makeOddString(str); // Check for Palindrome if (isPalindrome(odd)) document.write( "Yes" ); else document.write( "No" ); } // Driver Code // Given string var str = "ddwfefwde"; // Function Call checkOddlyPalindrome(str); // This code is contributed by itsok. </script>
Yes
Complejidad de tiempo: O(N) , N es la longitud de la string.
Espacio Auxiliar: O(N/2)
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