📜  替换后的最小回文数

📅  最后修改于: 2022-05-13 01:57:08.821000             🧑  作者: Mango

替换后的最小回文数

给定一个包含一些小写字母字符和一个特殊字符点(.)的字符串。我们需要用一些字母字符替换所有点,以使结果字符串成为回文,在许多可能的替换的情况下,我们需要选择字典上最小的回文字符串。如果在所有可能的替换之后无法将字符串转换为回文,则输出不可能。
例子:

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

我们可以这样解决这个问题,由于结果字符串需要是回文,我们可以在开始时检查一对非点字符,如果它们不匹配,那么直接返回是不可能的,因为我们可以在点的位置放置新字符只是在其他任何地方都没有。
之后,我们遍历字符的字符串,如果当前字符是点,那么我们检查它的配对字符(第 (n – i -1) 个位置的字符),如果该字符也是点,那么我们可以将两个字符替换为'a',因为 'a' 是最小的小写字母,这将保证最后的最小字典字符串,将两者替换为任何其他字符将导致字典上更大的回文字符串。在其他情况下,如果配对字符不是点,那么要使字符串回文,我们必须将当前字符替换为其配对字符。

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

上面的过程给了我们字典上最小的回文字符串。

C++
// C++ program to get lexicographically smallest
// palindrome string
#include 
using namespace std;
 
// Utility method to check str is possible palindrome
// after ignoring .
bool isPossiblePalindrome(string str)
{
    int n = str.length();
    for (int i=0; i


Java
// Java program to get lexicographically
// smallest palindrome string
class GFG
{
// Utility method to check str is
// possible palindrome after ignoring
static boolean isPossiblePalindrome(char str[])
{
int n = str.length;
for (int i = 0; i < n / 2; i++)
{
    /* If both left and right character
        are not dot and they are not
        equal also, then it is not
        possible to make this string a
        palindrome */
    if (str[i] != '.' &&
        str[n - i - 1] != '.' &&
        str[i] != str[n - i - 1])
        return false;
}
 
return true;
}
 
// Returns lexicographically smallest
// palindrome string, if possible
static void smallestPalindrome(char str[])
{
if (!isPossiblePalindrome(str))
    System.out.println("Not Possible");
 
int n = str.length;
 
// loop through character of string
for (int i = 0; i < n; i++)
{
    if (str[i] == '.')
    {
        // if one of character is dot,
        // replace dot with other character
        if (str[n - i - 1] != '.')
            str[i] = str[n - i - 1];
 
        // if both character are dot,
        // then replace them with
        // smallest character 'a'
        else
            str[i] = str[n - i - 1] = 'a';
    }
}
 
// return the result
for(int i = 0; i < n; i++)
    System.out.print(str[i] + "");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "ab..e.c.a";
    char[] s = str.toCharArray();
    smallestPalindrome(s);
}
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to get lexicographically
# smallest palindrome string
 
# Utility method to check str is
# possible palindrome after ignoring
def isPossiblePalindrome(str):
    n = len(str)
    for i in range(n // 2):
         
        # If both left and right character
        # are not dot and they are not
        # equal also, then it is not possible
        # to make this string a palindrome
        if (str[i] != '.' and
            str[n - i - 1] != '.' and
            str[i] != str[n - i - 1]):
            return False
 
    return True
 
# Returns lexicographically smallest
# palindrome string, if possible
def smallestPalindrome(str):
    if (not isPossiblePalindrome(str)):
        return "Not Possible"
 
    n = len(str)
    str = list(str)
     
    # loop through character of string
    for i in range(n):
        if (str[i] == '.'):
             
            # if one of character is dot,
            # replace dot with other character
            if (str[n - i - 1] != '.'):
                str[i] = str[n - i - 1]
 
            # if both character are dot,
            # then replace them with
            # smallest character 'a'
            else:
                str[i] = str[n - i - 1] = 'a'
 
    # return the result
    return str
 
# Driver code
if __name__ == "__main__":
    str = "ab..e.c.a"
    print(''.join(smallestPalindrome(str)))
 
# This code is contributed by ChitraNayal


C#
// C# program to get lexicographically
// smallest palindrome string
using System;
public class GFG
    {
    // Utility method to check str is
    // possible palindrome after ignoring
    static bool isPossiblePalindrome(char []str)
    {
    int n = str.Length;
    for (int i = 0; i < n / 2; i++)
    {
        /* If both left and right character
            are not dot and they are not
            equal also, then it is not
            possible to make this string a
            palindrome */
        if (str[i] != '.' &&
            str[n - i - 1] != '.' &&
            str[i] != str[n - i - 1])
            return false;
    }
 
    return true;
    }
 
    // Returns lexicographically smallest
    // palindrome string, if possible
    static void smallestPalindrome(char []str)
    {
    if (!isPossiblePalindrome(str))
        Console.WriteLine("Not Possible");
 
    int n = str.Length;
 
    // loop through character of string
    for (int i = 0; i < n; i++)
    {
        if (str[i] == '.')
        {
            // if one of character is dot,
            // replace dot with other character
            if (str[n - i - 1] != '.')
                str[i] = str[n - i - 1];
 
            // if both character are dot,
            // then replace them with
            // smallest character 'a'
            else
                str[i] = str[n - i - 1] = 'a';
        }
    }
 
    // return the result
    for(int i = 0; i < n; i++)
       Console.Write(str[i] + "");
    }
 
    // Driver code
    public static void Main()
    {
        String str = "ab..e.c.a";
        char[] s = str.ToCharArray();
        smallestPalindrome(s);
    }
}
// This code is contributed by PrinciRaj1992


PHP


Javascript


输出:

abcaeacba