📜  将字符串转换为回文的最低成本

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

将字符串转换为回文的最低成本

将字符串S 转换为回文字符串。您只能将一个字符替换为任何其他字符。当您将字符'a' 替换为任何其他字符时,它花费 1 个单位,类似地,对于 'b' 它是 2 个单位......而对于 'z',它是 26 个单位。找到将字符串S 转换为回文字符串所需的最小成本。
例子 :

Input : abcdef
Output : 6
Explanation: replace 'a', 'b' and 
'c' => cost= 1 + 2 + 3 = 6 

Input : aba
Output : 0

这个想法是从字符串的两端开始比较。让i被初始化为 0 索引, j被初始化为长度 - 1。如果两个索引处的字符不同,则将应用成本。为了使成本最小,替换较小的字符。然后将i增加 1 并将j减少 1。迭代直到i小于j

C++
// CPP program to find minimum cost to make
// a palindrome.
#include 
using namespace std;
 
// Function to return cost
int cost(string str)
{
    // length of string
    int len = str.length();     
     
    // Iterate from  both sides of string.
    // If not equal, a cost will be there
    int res = 0;
    for (int i=0, j=len-1; i < j; i++, j--)        
        if (str[i] != str[j])
            res += min(str[i], str[j]) - 'a' + 1;       
     
    return res;
}
 
// Driver code
int main()
{
    string str = "abcdef";
    cout << cost(str) << endl;
    return 0;
}


Java
// Java program to find minimum cost to make
// a palindrome.
import java.io.*;
 
class GFG
{
    // Function to return cost
    static int cost(String str)
    {
        // length of string
        int len = str.length();
         
        // Iterate from both sides of string.
        // If not equal, a cost will be there
        int res = 0;
        for (int i = 0, j = len - 1; i < j; i++, j--)    
            if (str.charAt(i) != str.charAt(j))
                res += Math.min(str.charAt(i), str.charAt(j))
                                - 'a' + 1;    
         
        return res;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String str = "abcdef";
        System.out.println(cost(str));
    }
}
 
// This code is contributed by vt_m.


Python3
# python program to find minimum
# cost to make a palindrome.
 
# Function to return cost
def cost(st):
     
    # length of string
    l = len(st)    
     
    # Iterate from both sides
    # of string. If not equal,
    # a cost will be there
    res = 0
    j = l - 1
    i = 0
    while(i < j):        
        if (st[i] != st[j]):
            res += (min(ord(st[i]),
                    ord(st[j])) -
                     ord('a') + 1)
             
        i = i + 1
        j = j - 1
         
    return res
 
# Driver code
st = "abcdef";
print(cost(st))
 
# This code is contributed by
# Sam007


C#
// C# program to find minimum cost
// to make a palindrome.
using System;
 
class GFG
{
     
    // Function to return cost
    static int cost(String str)
    {
        // length of string
        int len = str.Length;
         
        // Iterate from both sides of string.
        // If not equal, a cost will be there
        int res = 0;
        for (int i = 0, j = len - 1; i < j; i++, j--)    
            if (str[i] != str[j])
                res += Math.Min(str[i], str[j])
                                - 'a' + 1;
         
        return res;
    }
     
    // Driver code
    public static void Main ()
    {
        string str = "abcdef";
        Console.WriteLine(cost(str));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

6