Dada una string palindrómica str y un entero N . La tarea es encontrar si es posible eliminar exactamente N caracteres de la string dada de modo que la string siga siendo un palíndromo.
Ejemplos:
Entrada: str = “abba”, N = 1
Salida: Sí
Quite ‘b’ y la string remanente
“aba” sigue siendo un palíndromo.
Entrada: str = “aba”, N = 1
Salida: Sí
Enfoque: se puede observar que siempre es posible eliminar cualquier número de caracteres menor o igual a su longitud de una string palindrómica de modo que la string resultante siga siendo una string palindrómica.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if str // remains a palindrome after removing // exactly N characters from it bool isPossible(string str, int n) { // Find the length of the string int len = str.length(); // If it is possible if (len >= n) return true; return false; } // Driver code int main() { string str = "abccba"; int n = 4; if (isPossible(str, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach class GFG { // Function that returns true if str // remains a palindrome after removing // exactly N characters from it static boolean isPossible(String str, int n) { // Find the length of the string int len = str.length(); // If it is possible if (len >= n) return true; return false; } // Driver code public static void main (String[] args) { String str = "abccba"; int n = 4; if (isPossible(str, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python
# Python3 implementation of the approach # Function that returns true if str # remains a palindrome after removing # exactly N characters from it def isPossible(str, n): # Find the length of the string l = len(str) # If it is possible if (l >= n): return True return False # Driver code str = "abccba" n = 4 if(isPossible(str, n)): print("Yes") else: print("No")
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if str // remains a palindrome after removing // exactly N characters from it static bool isPossible(String str, int n) { // Find the length of the string int len = str.Length; // If it is possible if (len >= n) return true; return false; } // Driver code public static void Main(String[] args) { String str = "abccba"; int n = 4; if (isPossible(str, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // Function that returns true if str // remains a palindrome after removing // exactly N characters from it function isPossible(str, n) { // Find the length of the string var len = str.length; // If it is possible if (len >= n) return true; return false; } var str = "abccba"; var n = 4; if (isPossible(str, n)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad de tiempo: O(1)
Publicación traducida automáticamente
Artículo escrito por Shubham_Chauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA