Operaciones de reducción mínima para convertir una string dada en un palíndromo

Dada una string, encuentre el número mínimo de operaciones de reducción requeridas para convertir una string dada en un palíndromo. En una operación de reducción, podemos cambiar el carácter a un valor inferior inmediato. Por ejemplo b se puede cubrir a a.

Ejemplos: 

C++

// CPP program to count minimum reduce
// operations to make a palindrome
#include <bits/stdc++.h>
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

<?php
// PHP program to count minimum
// reduce operations to make a
// palindrome
 
// Returns count of minimum
// character reduce operations
// to make palindrome.
function countReduce($str)
{
    $n = strlen($str);
    $res = 0;
 
    // Compare every character
    // of first half with the
    // corresponding character
    // of second half and add
    // difference to result.
    for ($i = 0; $i < $n / 2; $i++)
        $res += abs(ord($str[$i]) -
                    ord($str[($n - $i - 1)]));
    return $res;
}
 
// Driver code
$str = "abcd";
echo countReduce($str);
 
// This code is contributed by Sam007
?>

Javascript

<script>
    // Javascript program to count minimum reduce
    // operations to make a palindrome
     
    // Returns count of minimum character
    // reduce operations to make palindrome.
    function countReduce(str)
    {
        let n = str.length;
        let res = 0;
       
        // Compare every character of first
        // half with the corresponding
        // character of second half and
        // add difference to result.
        for (let i = 0; i < parseInt(n / 2, 10); i++)
            res += Math.abs(str[i].charCodeAt() - str[n - i - 1].charCodeAt());
       
        return res;
    }
        
      let str = "abcd";
      document.write(countReduce(str));
     
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *