Make a string non-palindromic by inserting a given character Last Updated : 20 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a string S and a character X, the task is to generate a non-palindromic string by inserting the character X in the string S. If it is not possible to obtain a non-palindromic string, then print "-1". Examples: Input: S = “ababab”, X = 'a'Output: “aababab”Explanation: Inserting the character 'a' at the beginning of the string S modifies the string to "aababab", which is non-palindromic. Input: S = “rrr”, X = 'r'Output: -1 Approach: The given problem can be solved based on the following observation that if the string contains only character X, then it is impossible to make the string non-palindromic. Otherwise, the string can be made non-palindromic. Follow the steps below to solve the problem: If the count of character X in the string S is the same as the length of the string S, then print "-1".Otherwise, check if concatenations of string S and character X is palindromic or not. If found to be true, then print the string S + X. Otherwise print (X + S). Below is the implementation of the above approach: 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> Output: sgeek Time Complexity: O(N)Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Make a string non-palindromic by inserting a given character dinijain99 Follow Improve Article Tags : Misc Strings DSA palindrome frequency-counting +1 More Practice Tags : MiscpalindromeStrings Similar Reads Remove a character from a string to make it a palindrome Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this. Examples: Input : str = âabcbaâ Output : Yes we can remove character âcâ to make string palindrome Input : str = âabcbeaâ Output : Yes we can remove character âeâ 10 min read Palindromic strings of length 3 possible by using characters of a given string Given a string S consisting of N characters, the task is to print all palindromic strings of length 3 in lexicographical order that can be formed using characters of the given string S. Examples: Input: S = "aabc"Output:abaaca Input: S = "ddadbac"Output:abaacaadadaddbddcdddd Approach: The given prob 9 min read Minimum number of characters to be replaced to make a given string Palindrome Given string str, the task is to find the minimum number of characters to be replaced to make a given string palindrome. Replacing a character means replacing it with any single character in the same position. We are not allowed to remove or add any characters. If there are multiple answers, print t 5 min read Palindrome by swapping only one character Given a string, the task is to check if the string can be made palindrome by swapping a character only once. [NOTE: only one swap and only one character should be swapped with another character] Examples: Input : bbg Output : true Explanation: Swap b(1st index) with g. Input : bdababd Output : true 9 min read Check if a given string is a rotation of a palindrome Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p 15+ min read Longest Substring that can be made a palindrome by swapping of characters Given a numeric string S, the task is to find the longest non-empty substring that can be made palindrome. Examples: Input: S = "3242415"Output: 5Explanation: "24241" is the longest such substring which can be converted to the palindromic string "24142". Input: S = "213123"Output: 6Explanation: "213 12 min read Convert the string into palindrome string by changing only one character Given a string str. Check if it is possible to convert the string into palindrome string by changing only one character.Examples: Input : str = "abccaa" Output : Yes We can change the second last character i.e. 'a' to 'b' to make it palindrome string Input : str = "abbcca" Output : No We can not con 5 min read Check if a given string is Even-Odd Palindrome or not Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab" 7 min read Make a palindromic string from given string Given a string S consisting of lower-case English alphabets only, we have two players playing the game. The rules are as follows: The player can remove any character from the given string S and write it on paper on any side(left or right) of an empty string.The player wins the game, if at any move h 7 min read Like