Dada una string S y un carácter X , la tarea es generar una string no palindrómica insertando el carácter X en la string S. Si no es posible obtener una string no palindrómica, imprima “-1” .
Ejemplos:
Entrada: S = “ababab”, X = ‘a’
Salida: “aababab”
Explicación: Insertar el carácter ‘a’ al comienzo de la string S modifica la string a “aababab”, que no es palindrómico.Entrada: S = “rrr”, X = ‘r’
Salida: -1
Enfoque: el problema dado se puede resolver en función de la siguiente observación de que si la string contiene solo el carácter X , entonces es imposible hacer que la string no sea palindrómica. De lo contrario, la cuerda puede hacerse no palindrómica. Siga los pasos a continuación para resolver el problema:
- Si la cuenta del carácter X en la string S es la misma que la longitud de la string S , imprima “-1” .
- De lo contrario, verifique si las concatenaciones de la string S y el carácter X son palindrómicas o no . Si se encuentra que es verdadero , imprima la string S + X. De lo contrario, imprima (X + S) .
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 palindromic or not bool Palindrome(string str) { // Traverse the string str for (int i = 0, j = str.length() - 1; i < j; i++, j--) { // Check if i-th character from // both ends are the same or not if (str[i] != str[j]) return false; } // Return true, as str is palindrome return true; } // Function to make the non-palindromic // string by inserting the character X void NonPalindrome(string str, char X) { // If all the characters // in the string are X if (count(str.begin(), str.end(), X) == str.length()) { cout << "-1"; return; } // Check if X + str is // palindromic or not if (Palindrome(X + str)) cout << str + X << endl; else cout << X + str << endl; } // Driver Code int main() { string S = "geek"; char X = 's'; NonPalindrome(S, X); return 0; }
Java
// java program for the above approach import java.io.*; import java.lang.*; import java.util.*; public class GFG { // Function to check if a // string is palindromic or not static boolean Palindrome(String str) { // Traverse the string str for (int i = 0, j = str.length() - 1; i < j; i++, j--) { // Check if i-th character from // both ends are the same or not if (str.charAt(i) != str.charAt(j)) return false; } // Return true, as str is palindrome return true; } // Function to make the non-palindromic // string by inserting the character X static void NonPalindrome(String str, char X) { // stores the count of char X in str int count = 0; for (int i = 0; i < str.length(); i++) if (str.charAt(i) == X) count++; // If all the characters // in the string are X if (count == str.length()) { System.out.println("-1"); return; } // Check if X + str is // palindromic or not if (Palindrome(X + str)) System.out.println(str + X); else System.out.println(X + str); } // Driver Code public static void main(String[] args) { String S = "geek"; char X = 's'; NonPalindrome(S, X); } } // This code is contributed by Kingash.
Python3
# Python3 program for the above approach # Function to check if a # string is palindromic or not def Palindrome(str): if str == str[::-1]: return True # Return true, as str is palindrome return False # Function to make the non-palindromic # string by inserting the character X def NonPalindrome(str, X): # If all the characters # in the string are X if (str.count(X) == len(str)): print("-1") return # Check if X + str is # palindromic or not if (Palindrome(X + str)): print(str + X) else: print(X + str) # Driver Code if __name__ == '__main__': S = "geek" X = 's' NonPalindrome(S, X) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; using System.Linq; class GFG { // Function to check if a // string is palindromic or not static bool Palindrome(string str) { // Traverse the string str for (int i = 0, j = str.Length - 1; i < j; i++, j--) { // Check if i-th character from // both ends are the same or not if (str[i] != str[j]) return false; } // Return true, as str is palindrome return true; } // Function to make the non-palindromic // string by inserting the character X static void NonPalindrome(string str, char X) { // If all the characters // in the string are X if (str.Count(p => p == X) == str.Length) { Console.Write("-1"); return; } // Check if X + str is // palindromic or not if (Palindrome(X + str)) Console.WriteLine(str + X); else Console.WriteLine(X + str); } // Driver Code public static void Main() { string S = "geek"; char X = 's'; NonPalindrome(S, X); } } // This code is contributed by ukasp.
Javascript
<script> // javascript program for the above approach // Function to check if a // string is palindromic or not function Palindrome( str) { // Traverse the string str for (let i = 0, j = str.length - 1; i < j; i++, j--) { // Check if i-th character from // both ends are the same or not if (str.charAt(i) != str.charAt(j)) return false; } // Return true, as str is palindrome return true; } // Function to make the non-palindromic // string by inserting the character X function NonPalindrome( str, X) { // stores the count of char X in str var count = 0; for (let i = 0; i < str.length; i++) if (str.charAt(i) == X) count++; // If all the characters // in the string are X if (count == str.length) { document.write("-1"); return; } // Check if X + str is // palindromic or not if (Palindrome(X + str)) document.write(str + X); else document.write(X + str); } // Driver Code var S = "geek"; var X = 's'; NonPalindrome(S, X); </script>
Producción:
sgeek
Complejidad de Tiempo: O(N)
Espacio Auxiliar: O(1), ya que no se ha tomado espacio extra.
Publicación traducida automáticamente
Artículo escrito por dinijain99 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA