📌  相关文章
📜  将给定字符串转换为回文的最小化简操作

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

将给定字符串转换为回文的最小化简操作

给定一个字符串,找出将给定字符串转换为回文所需的最小化简操作数。在 reduce 操作中,我们可以将字符更改为立即较低的值。例如 b 可以覆盖到 a。
例子 :

Input  :  abcd  
Output :  4
We need to reduce c once
and d three times.

Input  : ccc
Output : 0

这个想法很简单。我们从左边遍历字符串,并将左半部分的字符与右半部分的对应字符进行比较。我们将字符之间的差异添加到结果中。

C++
// CPP program to count minimum reduce
// operations to make a palindrome
#include 
using namespace std;
 
// Returns count of minimum character
// reduce operations to make palindrome.
int countReduce(string& str)
{
    int n = str.length();
    int res = 0;
 
    // Compare every character of first half
    // with the corresponding character of
    // second half and add difference to
    // result.
    for (int i = 0; i < n / 2; i++)
        res += abs(str[i] - str[n - i - 1]);
 
    return res;
}
 
// Driver code
int main()
{
    string str = "abcd";
    cout << countReduce(str);
    return 0;
}


Java
// Java program to count minimum reduce
// operations to make a palindrome
import java.io.*;
 
class GFG
{
    // Returns count of minimum character
    // reduce operations to make palindrome.
    static int countReduce(String str)
    {
        int n = str.length();
        int res = 0;
     
        // Compare every character of first half
        // with the corresponding character of
        // second half and add difference to
        // result.
        for (int i = 0; i < n / 2; i++)
            res += Math.abs(str.charAt(i)
                   - str.charAt(n - i - 1));
     
        return res;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String str = "abcd";
        System.out.println( countReduce(str));
             
    }
}
 
// This code is contributed by vt_m.


Python3
# python3 program to count minimum reduce
# operations to make a palindrome
 
# Returns count of minimum character
# reduce operations to make palindrome.
def countReduce(str):
 
    n = len(str)
    res = 0
 
    # Compare every character of first half
    # with the corresponding character of
    # second half and add difference to
    # result.
    for i in range(0, int(n/2)):
        res += abs( int(ord(str[i])) -
               int(ord(str[n - i - 1])) )
     
    return res
 
# Driver code
str = "abcd"
print(countReduce(str))
 
# This code is contributed by Sam007


C#
// C# program to count minimum reduce
// operations to make a palindrome
using System;
 
class GFG {
     
    // Returns count of minimum character
    // reduce operations to make palindrome.
    static int countReduce(string str)
    {
        int n = str.Length;
        int res = 0;
     
        // Compare every character of first
        // half with the corresponding
        // character of second half and
        // add difference to result.
        for (int i = 0; i < n / 2; i++)
            res += Math.Abs(str[i]
                    - str[n - i - 1]);
     
        return res;
    }
     
    // Driver code
    public static void Main ()
    {
        string str = "abcd";
        Console.WriteLine( countReduce(str));
             
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

4