Dada una string str , la tarea es eliminar todos los duplicados consecutivos de la string str y verificar si la string final es palíndromo o no. Escriba “Sí” si es un palindrómico, de lo contrario escriba “No” .
Ejemplos:
Entrada: str = “abbcbbbaaa”
Salida: Sí
Explicación:
Al eliminar todos los caracteres duplicados consecutivos, la string se convierte en “abcba”, que es un palíndromo.
Entrada: str = “aaabbbaaccc”
Salida: No
Explicación:
Al eliminar todos los caracteres duplicados consecutivos, la string se convierte en “abac”, que no es un palíndromo.
Enfoque: La idea es crear una nueva string a partir de la string dada y verificar si la nueva string es palindrómica o no. A continuación se muestran los pasos:
- Inicialice la nueva string newStr = “” .
- Repita todos los caracteres de la string dada uno por uno y si el carácter actual es diferente del carácter anterior, agregue este carácter a la nueva string newStr .
- De lo contrario, busque el siguiente carácter.
- Compruebe si la string final formada es palíndromo o no. Escriba “Sí” si es un palindrómico, de lo contrario escriba “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 a string // is palindrome or not bool isPalindrome(string str) { // Length of the string int len = str.length(); // Check if its a palindrome for (int i = 0; i < len; i++) { // If the palindromic // condition is not met if (str[i] != str[len - i - 1]) return false; } // Return true as str is palindromic return true; } // Function to check if string str is // palindromic after removing every // consecutive characters from the str bool isCompressablePalindrome(string str) { // Length of the string str int len = str.length(); // Create an empty // compressed string string compressed = ""; // The first character will // always be included in // the final string compressed.push_back(str[0]); // Check all the characters // of the string for (int i = 1; i < len; i++) { // If the current character // is not same as its previous // one, then insert it in the // final string if (str[i] != str[i - 1]) compressed.push_back(str[i]); } // Check if the compressed // string is a palindrome return isPalindrome(compressed); } // Driver Code int main() { // Given string string str = "abbcbbbaaa"; // Function call if (isCompressablePalindrome(str)) cout << "Yes\n"; else cout << "No\n"; return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to check if a String // is palindrome or not static boolean isPalindrome(String str) { // Length of the String int len = str.length(); // Check if its a palindrome for (int i = 0; i < len; i++) { // If the palindromic // condition is not met if (str.charAt(i) != str.charAt(len - i - 1)) return false; } // Return true as str is palindromic return true; } // Function to check if String str is // palindromic after removing every // consecutive characters from the str static boolean isCompressablePalindrome(String str) { // Length of the String str int len = str.length(); // Create an empty // compressed String String compressed = ""; // The first character will // always be included in // the final String compressed = String.valueOf(str.charAt(0)); // Check all the characters // of the String for (int i = 1; i < len; i++) { // If the current character // is not same as its previous // one, then insert it in the // final String if (str.charAt(i) != str.charAt(i - 1)) compressed += str.charAt(i); } // Check if the compressed // String is a palindrome return isPalindrome(compressed); } // Driver Code public static void main(String[] args) { // Given String String str = "abbcbbbaaa"; // Function call if (isCompressablePalindrome(str)) System.out.print("Yes\n"); else System.out.print("No\n"); } } // This code is contributed by amal kumar choubey
Python3
# Python3 program for the above approach # Function to check if a string # is palindrome or not def isPalindrome(Str): # Length of the string Len = len(Str) # Check if its a palindrome for i in range(Len): # If the palindromic # condition is not met if (Str[i] != Str[Len - i - 1]): return False # Return true as Str is palindromic return True # Function to check if string str is # palindromic after removing every # consecutive characters from the str def isCompressablePalindrome(Str): # Length of the string str Len = len(Str) # Create an empty compressed string compressed = "" # The first character will always # be included in final string compressed += Str[0] # Check all the characters # of the string for i in range(1, Len): # If the current character # is not same as its previous # one, then insert it in # the final string if (Str[i] != Str[i - 1]): compressed += Str[i] # Check if the compressed # string is a palindrome return isPalindrome(compressed) # Driver Code if __name__ == '__main__': # Given string Str = "abbcbbbaaa" # Function call if (isCompressablePalindrome(Str)): print("Yes") else: print("No") # This code is contributed by himanshu77
C#
// C# program for the above approach using System; class GFG{ // Function to check if a String // is palindrome or not static bool isPalindrome(String str) { // Length of the String int len = str.Length; // Check if its a palindrome for(int i = 0; i < len; i++) { // If the palindromic // condition is not met if (str[i] != str[len - i - 1]) return false; } // Return true as str is palindromic return true; } // Function to check if String str is // palindromic after removing every // consecutive characters from the str static bool isCompressablePalindrome(String str) { // Length of the String str int len = str.Length; // Create an empty // compressed String String compressed = ""; // The first character will // always be included in // the readonly String compressed = String.Join("", str[0]); // Check all the characters // of the String for(int i = 1; i < len; i++) { // If the current character // is not same as its previous // one, then insert it in the // readonly String if (str[i] != str[i - 1]) compressed += str[i]; } // Check if the compressed // String is a palindrome return isPalindrome(compressed); } // Driver Code public static void Main(String[] args) { // Given String String str = "abbcbbbaaa"; // Function call if (isCompressablePalindrome(str)) Console.Write("Yes\n"); else Console.Write("No\n"); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript program for the above approach // Function to check if a String // is palindrome or not function isPalindrome(str) { // Length of the String let len = str.length; // Check if its a palindrome for(let i = 0; i < len; i++) { // If the palindromic // condition is not met if (str[i] != str[len - i - 1]) return false; } // Return true as str is palindromic return true; } // Function to check if String str is // palindromic after removing every // consecutive characters from the str function isCompressablePalindrome(str) { // Length of the String str let len = str.length; // Create an empty // compressed String let compressed = ""; // The first character will // always be included in // the readonly String compressed = str[0]; // Check all the characters // of the String for(let i = 1; i < len; i++) { // If the current character // is not same as its previous // one, then insert it in the // readonly String if (str[i] != str[i - 1]) compressed += str[i]; } // Check if the compressed // String is a palindrome return isPalindrome(compressed); } // Given String let str = "abbcbbbaaa"; // Function call if (isCompressablePalindrome(str)) document.write("Yes" + "</br>"); else document.write("No"); // This code is contributed by divyeshrabadiya07. </script>
Yes
Complejidad de tiempo: O(N) , donde N es la longitud de la string.
Publicación traducida automáticamente
Artículo escrito por lostsoul27 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA