Make lexicographically smallest palindrome by substituting missing characters
Last Updated :
23 Dec, 2022
Given a string str some of whose characters are missing and are represented by a ‘*’. The task is to substitute the missing characters so as to make the lexicographically smallest palindrome. If it ot possible to make the string palindrome then print -1.
Examples:
Input: str = “ab*a”
Output: abba
Input: a*b
Output: -1
We can’t make it palindrome so output is -1.
Approach:
- Place the ‘i’ marker at the starting of string and ‘j’ marker at the end of the string.
- If characters at both the positions are missing then substitute both the characters with ‘a’ so as to make it lexicographically smallest palindrome.
- If character at only ith or jth position is missing then replace it with jth or ith character respectively.
- If character at ith and jth positions are not equal then the string cannot be made into a palindrome and print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string makePalindrome(string str)
{
int i = 0, j = str.length() - 1;
while (i <= j) {
if (str[i] == '*' && str[j] == '*' ) {
str[i] = 'a' ;
str[j] = 'a' ;
}
else if (str[j] == '*' )
str[j] = str[i];
else if (str[i] == '*' )
str[i] = str[j];
else if (str[i] != str[j])
return "-1" ;
i++;
j--;
}
return str;
}
int main()
{
string str = "na*an" ;
cout << makePalindrome(str);
return 0;
}
|
Java
class GFG
{
static String makePalindrome( char [] str)
{
int i = 0 , j = str.length - 1 ;
while (i <= j)
{
if (str[i] == '*' && str[j] == '*' )
{
str[i] = 'a' ;
str[j] = 'a' ;
}
else if (str[j] == '*' )
str[j] = str[i];
else if (str[i] == '*' )
str[i] = str[j];
else if (str[i] != str[j])
return "-1" ;
i++;
j--;
}
return String.valueOf(str);
}
public static void main(String[] args)
{
char [] str = "na*an" .toCharArray();
System.out.println(makePalindrome(str));
}
}
|
Python3
def makePalindrome(str1):
i = 0
j = len (str1) - 1
str1 = list (str1)
while (i < = j):
if (str1[i] = = '*' and str1[j] = = '*' ):
str1[i] = 'a'
str1[j] = 'a'
elif (str1[j] = = '*' ):
str1[j] = str1[i]
elif (str1[i] = = '*' ):
str1[i] = str1[j]
elif (str1[i] ! = str1[j]):
str1 = '' . join(str1)
return "-1"
i + = 1
j - = 1
str1 = '' . join(str1)
return str1
if __name__ = = '__main__' :
str1 = "na*an"
print (makePalindrome(str1))
|
C#
using System;
class GFG
{
static String makePalindrome( char [] str)
{
int i = 0, j = str.Length - 1;
while (i <= j)
{
if (str[i] == '*' && str[j] == '*' )
{
str[i] = 'a' ;
str[j] = 'a' ;
}
else if (str[j] == '*' )
str[j] = str[i];
else if (str[i] == '*' )
str[i] = str[j];
else if (str[i] != str[j])
return "-1" ;
i++;
j--;
}
return String.Join( "" ,str);
}
public static void Main(String[] args)
{
char [] str = "na*an" .ToCharArray();
Console.WriteLine(makePalindrome(str));
}
}
|
PHP
<?php
function makePalindrome( $str )
{
$i = 0; $j = strlen ( $str ) - 1;
while ( $i <= $j )
{
if ( $str [ $i ] == '*' && $str [ $j ] == '*' )
{
$str [ $i ] = 'a' ;
$str [ $j ] = 'a' ;
}
else if ( $str [ $j ] == '*' )
$str [ $j ] = $str [ $i ];
else if ( $str [ $i ] == '*' )
$str [ $i ] = $str [ $j ];
else if ( $str [ $i ] != $str [ $j ])
return "-1" ;
$i ++;
$j --;
}
return $str ;
}
$str = "na*an" ;
echo makePalindrome( $str );
?>
|
Javascript
<script>
function makePalindrome(str)
{
var i = 0, j = str.length - 1;
while (i <= j) {
if (str[i] == '*' && str[j] == '*' ) {
str[i] = 'a' ;
str[j] = 'a' ;
}
else if (str[j] == '*' )
str[j] = str[i];
else if (str[i] == '*' )
str[i] = str[j];
else if (str[i] != str[j])
return "-1" ;
i++;
j--;
}
return str.join( "" );
}
var str = "na*an" .split( '' );
document.write(makePalindrome(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...