Make a string non-palindromic by inserting a given character
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++
#include <bits/stdc++.h>
using namespace std;
bool Palindrome(string str)
{
for ( int i = 0, j = str.length() - 1;
i < j; i++, j--) {
if (str[i] != str[j])
return false ;
}
return true ;
}
void NonPalindrome(string str, char X)
{
if (count(str.begin(), str.end(), X)
== str.length()) {
cout << "-1" ;
return ;
}
if (Palindrome(X + str))
cout << str + X << endl;
else
cout << X + str << endl;
}
int main()
{
string S = "geek" ;
char X = 's' ;
NonPalindrome(S, X);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static boolean Palindrome(String str)
{
for ( int i = 0 , j = str.length() - 1 ; i < j;
i++, j--) {
if (str.charAt(i) != str.charAt(j))
return false ;
}
return true ;
}
static void NonPalindrome(String str, char X)
{
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++)
if (str.charAt(i) == X)
count++;
if (count == str.length()) {
System.out.println( "-1" );
return ;
}
if (Palindrome(X + str))
System.out.println(str + X);
else
System.out.println(X + str);
}
public static void main(String[] args)
{
String S = "geek" ;
char X = 's' ;
NonPalindrome(S, X);
}
}
|
Python3
def Palindrome( str ):
if str = = str [:: - 1 ]:
return True
return False
def NonPalindrome( str , X):
if ( str .count(X) = = len ( str )):
print ( "-1" )
return
if (Palindrome(X + str )):
print ( str + X)
else :
print (X + str )
if __name__ = = '__main__' :
S = "geek"
X = 's'
NonPalindrome(S, X)
|
C#
using System;
using System.Linq;
class GFG {
static bool Palindrome( string str)
{
for ( int i = 0, j = str.Length - 1; i < j;
i++, j--) {
if (str[i] != str[j])
return false ;
}
return true ;
}
static void NonPalindrome( string str, char X)
{
if (str.Count(p => p == X) == str.Length) {
Console.Write( "-1" );
return ;
}
if (Palindrome(X + str))
Console.WriteLine(str + X);
else
Console.WriteLine(X + str);
}
public static void Main()
{
string S = "geek" ;
char X = 's' ;
NonPalindrome(S, X);
}
}
|
Javascript
<script>
function Palindrome( str)
{
for (let i = 0, j = str.length - 1; i < j;
i++, j--) {
if (str.charAt(i) != str.charAt(j))
return false ;
}
return true ;
}
function NonPalindrome( str, X)
{
var count = 0;
for (let i = 0; i < str.length; i++)
if (str.charAt(i) == X)
count++;
if (count == str.length) {
document.write( "-1" );
return ;
}
if (Palindrome(X + str))
document.write(str + X);
else
document.write(X + str);
}
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.
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...