Smallest Palindrome after replacement
Last Updated :
06 Jul, 2022
Given a string which has some lowercase alphabet characters and one special character dot(.). We need to replace all dots with some alphabet character in such a way that resultant string becomes a palindrome, in case of many possible replacements, we need to choose palindrome string which is lexicographically smallest. If it is not possible to convert string into palindrome after all possible replacements then output Not possible.
Examples:
Input : str = “ab..e.c.a”
Output : abcaeacba
The smallest palindrome which can be made
after replacement is "abcaeacba"
We replaced first dot with "c", second dot with
"a", third dot with "a" and fourth dot with "b"
Input : str = “ab..e.c.b”
Output : Not Possible
It is not possible to convert above string into
palindrome
We can solve this problem as follows, As resultant string need to be palindrome, we can check pair of non-dot characters in starting itself, if they don’t match then direct return not possible because we can place new character at position of dots only not anywhere else.
After that, we iterate over characters of string, if current character is dot, then we check its paired character (character at (n – i -1)th position), if that character is also dot, then we can replace both character by ‘a’, because ‘a’ is smallest lowercase alphabet which will guarantee smallest lexicographic string at the end, replacing both by any other character will result in lexicographically larger palindromic string. In other case, if paired character is not a dot, then to make string palindrome we must replace current character by its paired character.
So in short,
If both "i", and "n- i- 1" are dot, replace them by ‘a’
If one of them is a dot character replace that by other non-dot character
Above procedure gives us lexicographically smallest palindrome string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossiblePalindrome(string str)
{
int n = str.length();
for ( int i=0; i<n/2; i++)
{
if (str[i] != '.' &&
str[n-i-1] != '.' &&
str[i] != str[n-i-1])
return false ;
}
return true ;
}
string smallestPalindrome(string str)
{
if (!isPossiblePalindrome(str))
return "Not Possible" ;
int n = str.length();
for ( int i = 0; i < n; i++)
{
if (str[i] == '.' )
{
if (str[n - i - 1] != '.' )
str[i] = str[n - i - 1];
else
str[i] = str[n - i - 1] = 'a' ;
}
}
return str;
}
int main()
{
string str = "ab..e.c.a" ;
cout << smallestPalindrome(str) << endl;
return 0;
}
|
Java
class GFG
{
static boolean isPossiblePalindrome( char str[])
{
int n = str.length;
for ( int i = 0 ; i < n / 2 ; i++)
{
if (str[i] != '.' &&
str[n - i - 1 ] != '.' &&
str[i] != str[n - i - 1 ])
return false ;
}
return true ;
}
static void smallestPalindrome( char str[])
{
if (!isPossiblePalindrome(str))
System.out.println( "Not Possible" );
int n = str.length;
for ( int i = 0 ; i < n; i++)
{
if (str[i] == '.' )
{
if (str[n - i - 1 ] != '.' )
str[i] = str[n - i - 1 ];
else
str[i] = str[n - i - 1 ] = 'a' ;
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(str[i] + "" );
}
public static void main(String[] args)
{
String str = "ab..e.c.a" ;
char [] s = str.toCharArray();
smallestPalindrome(s);
}
}
|
Python 3
def isPossiblePalindrome( str ):
n = len ( str )
for i in range (n / / 2 ):
if ( str [i] ! = '.' and
str [n - i - 1 ] ! = '.' and
str [i] ! = str [n - i - 1 ]):
return False
return True
def smallestPalindrome( str ):
if ( not isPossiblePalindrome( str )):
return "Not Possible"
n = len ( str )
str = list ( str )
for i in range (n):
if ( str [i] = = '.' ):
if ( str [n - i - 1 ] ! = '.' ):
str [i] = str [n - i - 1 ]
else :
str [i] = str [n - i - 1 ] = 'a'
return str
if __name__ = = "__main__" :
str = "ab..e.c.a"
print (''.join(smallestPalindrome( str )))
|
C#
using System;
public class GFG
{
static bool isPossiblePalindrome( char []str)
{
int n = str.Length;
for ( int i = 0; i < n / 2; i++)
{
if (str[i] != '.' &&
str[n - i - 1] != '.' &&
str[i] != str[n - i - 1])
return false ;
}
return true ;
}
static void smallestPalindrome( char []str)
{
if (!isPossiblePalindrome(str))
Console.WriteLine( "Not Possible" );
int n = str.Length;
for ( int i = 0; i < n; i++)
{
if (str[i] == '.' )
{
if (str[n - i - 1] != '.' )
str[i] = str[n - i - 1];
else
str[i] = str[n - i - 1] = 'a' ;
}
}
for ( int i = 0; i < n; i++)
Console.Write(str[i] + "" );
}
public static void Main()
{
String str = "ab..e.c.a" ;
char [] s = str.ToCharArray();
smallestPalindrome(s);
}
}
|
PHP
<?php
function isPossiblePalindrome( $str )
{
$n = strlen ( $str );
for ( $i = 0; $i < $n / 2; $i ++)
{
if ( $str [ $i ] != '.' &&
$str [ $n - $i - 1] != '.' &&
$str [ $i ] != $str [ $n - $i - 1])
return false;
}
return true;
}
function smallestPalindrome( $str )
{
if (!isPossiblePalindrome( $str ))
return "Not Possible" ;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == '.' )
{
if ( $str [ $n - $i - 1] != '.' )
$str [ $i ] = $str [ $n - $i - 1];
else
$str [ $i ] = $str [ $n - $i - 1] = 'a' ;
}
}
return $str ;
}
$str = "ab..e.c.a" ;
echo smallestPalindrome( $str );
?>
|
Javascript
<script>
function isPossiblePalindrome(str)
{
let n = str.length;
for (let i = 0; i < Math.floor(n / 2); i++)
{
if (str[i] != '.' &&
str[n - i - 1] != '.' &&
str[i] != str[n - i - 1])
return false ;
}
return true ;
}
function smallestPalindrome(str)
{
if (!isPossiblePalindrome(str))
document.write( "Not Possible" );
let n = str.length;
for (let i = 0; i < n; i++)
{
if (str[i] == '.' )
{
if (str[n - i - 1] != '.' )
str[i] = str[n - i - 1];
else
str[i] = str[n - i - 1] = 'a' ;
}
}
for (let i = 0; i < n; i++)
document.write(str[i] + "" );
}
let str= "ab..e.c.a" ;
let s = str.split( "" );
smallestPalindrome(s);
</script>
|
Time complexity: O(n) where n is the length of the string.
Auxiliary Space complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...