Make the string lexicographically smallest and non palindromic by replacing exactly one character
Last Updated :
30 Dec, 2022
Given a palindromic string str containing only lowercase letters, the task is to print the lexicographically smallest string, by replacing exactly one character, such the string is not a palindrome.
Examples:
Input: str = “abccba”
Output: “aaccba”
Explanation:
Lexicographically smallest non-palindromic string possible is “aaccba”, here we have replaced the second letter ‘b’ by an ‘a’ which makes it a non palindrome.
Input: str = “a”
Output: -1
Explanation:
A single character is always a palindrome hence we cannot replace the value. Therefore the output is -1.
Approach: To solve the problem mentioned above we will check only half of the string and replace all characters which are not ‘a’ to the character ‘a’ itself. The edge case for the question is if there is only one character, we will return an empty string. Otherwise, if all the characters are the same then we will replace the last character only with character ‘b’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findStr(string S)
{
int n = S.size();
for ( int i = 0; i < n / 2; ++i) {
if (S[i] != 'a' ) {
S[i] = 'a' ;
return S;
}
}
S[n - 1] = 'b' ;
return n < 2 ? " -1 " : S;
}
int main()
{
string str = "a" ;
cout << findStr(str) << endl;
string str1 = "abccba" ;
cout << findStr(str1) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static String findStr(String S)
{
StringBuilder sb = new StringBuilder(S);
int n = sb.length();
for ( int i = 0 ; i < n / 2 ; ++i)
{
if (sb.charAt(i) != 'a' )
{
sb.setCharAt(i, 'a' );
return sb.toString();
}
}
sb.setCharAt(n - 1 , 'b' );
return n < 2 ? " -1 " : sb.toString();
}
public static void main(String[] args)
{
String str = "a" ;
System.out.println(findStr(str));
String str1 = "abccba" ;
System.out.println(findStr(str1));
}
}
|
Python3
def findStr(S):
S = list (S)
n = len (S)
for i in range ( 0 , n / / 2 ):
if S[i] ! = 'a' :
S[i] = 'a'
return (''.join(S))
S[n - 1 ] = 'b'
if n < 2 :
return '-1'
else :
return (''.join(S))
if __name__ = = '__main__' :
str1 = 'a'
print (findStr(str1))
str2 = 'abccba'
print (findStr(str2))
|
C#
using System;
class GFG{
static String findStr( char []S)
{
int n = S.Length;
for ( int i = 0; i < n / 2; ++i)
{
if (S[i] != 'a' )
{
S[i] = 'a' ;
return new String(S);
}
}
S[n - 1] = 'b' ;
return n < 2 ? " -1 " : new String(S);
}
public static void Main(String[] args)
{
String str = "a" ;
Console.WriteLine(findStr(str.ToCharArray()));
String str1 = "abccba" ;
Console.WriteLine(findStr(str1.ToCharArray()));
}
}
|
Javascript
<script>
function findStr(S)
{
var n = S.length;
for ( var i = 0; i < n / 2; ++i) {
if (S[i] != 'a' ) {
S[i] = 'a' ;
return S.join( "" );;
}
}
S[n - 1] = 'b' ;
return n < 2 ? " -1 " : S.join( "" );;
}
var str = "a" ;
document.write( findStr(Array.from(str)) + "<br>" );
var str1 = "abccba" ;
document.write( findStr(Array.from(str1)));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...