通过替换缺失的字符来制作字典上最小的回文
给定一个字符串str ,其中一些字符丢失并由'*'表示。任务是替换缺失的字符,以生成字典上最小的回文。如果无法生成字符串回文,则打印-1 。
例子:
Input: str = “ab*a”
Output: abba
Input: a*b
Output: -1
We can’t make it palindrome so output is -1.
方法:
- 将“i”标记放在字符串的开头,将“j”标记放在字符串的末尾。
- 如果两个位置的字符都丢失,则用“a”替换这两个字符,以使其在字典上最小的回文。
- 如果仅缺少第i个或第j个位置的字符,则分别用第 j个或第 i个字符替换它。
- 如果第 i个和第j个位置的字符不相等,则该字符串不能变成回文并打印-1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the lexicographically
// smallest palindrome that can be made from
// the given string after replacing
// the required characters
string makePalindrome(string str)
{
int i = 0, j = str.length() - 1;
while (i <= j) {
// If characters are missing at both the positions
// then substitute it with 'a'
if (str[i] == '*' && str[j] == '*') {
str[i] = 'a';
str[j] = 'a';
}
// If only str[j] = '*' then update it
// with the value at str[i]
else if (str[j] == '*')
str[j] = str[i];
// If only str[i] = '*' then update it
// with the value at str[j]
else if (str[i] == '*')
str[i] = str[j];
// If characters at both positions
// are not equal and != '*' then the string
// cannot be made palindrome
else if (str[i] != str[j])
return "-1";
i++;
j--;
}
// Return the required palindrome
return str;
}
// Driver code
int main()
{
string str = "na*an";
cout << makePalindrome(str);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the lexicographically
// smallest palindrome that can be made from
// the given string after replacing
// the required characters
static String makePalindrome(char[] str)
{
int i = 0, j = str.length - 1;
while (i <= j)
{
// If characters are missing at both the positions
// then substitute it with 'a'
if (str[i] == '*' && str[j] == '*')
{
str[i] = 'a';
str[j] = 'a';
}
// If only str[j] = '*' then update it
// with the value at str[i]
else if (str[j] == '*')
str[j] = str[i];
// If only str[i] = '*' then update it
// with the value at str[j]
else if (str[i] == '*')
str[i] = str[j];
// If characters at both positions
// are not equal and != '*' then the string
// cannot be made palindrome
else if (str[i] != str[j])
return "-1";
i++;
j--;
}
// Return the required palindrome
return String.valueOf(str);
}
// Driver code
public static void main(String[] args)
{
char[] str = "na*an".toCharArray();
System.out.println(makePalindrome(str));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function to return the lexicographically
# smallest palindrome that can be made from
# the given string after replacing
# the required characters
def makePalindrome(str1):
i = 0
j = len(str1) - 1
str1 = list(str1)
while (i <= j):
# If characters are missing
# at both the positions
# then substitute it with 'a'
if (str1[i] == '*' and str1[j] == '*'):
str1[i] = 'a'
str1[j] = 'a'
# If only str1[j] = '*' then update it
# with the value at str1[i]
elif (str1[j] == '*'):
str1[j] = str1[i]
# If only str1[i] = '*' then update it
# with the value at str1[j]
elif (str1[i] == '*'):
str1[i] = str1[j]
# If characters at both positions
# are not equal and != '*' then the string
# cannot be made palindrome
elif (str1[i] != str1[j]):
str1 = '' . join(str1)
return "-1"
i += 1
j -= 1
# Return the required palindrome
str1 = '' . join(str1)
return str1
# Driver code
if __name__ == '__main__':
str1 = "na*an"
print(makePalindrome(str1))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the lexicographically
// smallest palindrome that can be made from
// the given string after replacing
// the required characters
static String makePalindrome(char[] str)
{
int i = 0, j = str.Length - 1;
while (i <= j)
{
// If characters are missing at both the positions
// then substitute it with 'a'
if (str[i] == '*' && str[j] == '*')
{
str[i] = 'a';
str[j] = 'a';
}
// If only str[j] = '*' then update it
// with the value at str[i]
else if (str[j] == '*')
str[j] = str[i];
// If only str[i] = '*' then update it
// with the value at str[j]
else if (str[i] == '*')
str[i] = str[j];
// If characters at both positions
// are not equal and != '*' then the string
// cannot be made palindrome
else if (str[i] != str[j])
return "-1";
i++;
j--;
}
// Return the required palindrome
return String.Join("",str);
}
// Driver code
public static void Main(String[] args)
{
char[] str = "na*an".ToCharArray();
Console.WriteLine(makePalindrome(str));
}
}
// This code has been contributed by 29AjayKumar
PHP
Javascript
输出:
naaan