📌  相关文章
📜  尽量减少字符替换为其最接近的字母,以使字符串回文

📅  最后修改于: 2021-06-25 13:52:14             🧑  作者: Mango

给定长度为N的小写字母组成的字符串S ,任务是找到将给定字符串转换为回文的最小操作数。在一项操作中,选择任何字符并将其替换为下一个或上一个字母。

例子:

天真的方法:最简单的方法是生成所有可能的长度为N的字符串。然后检查每个字符串,如果它是回文。如果发现任何字符串都是回文的,则找出将给定字符串转换为该字符串所需的操作成本。对所有生成的字符串重复上述步骤,并打印所有成本中计算出的最低成本。

时间复杂度: O(26 N )
辅助空间: O(N)

高效方法:为了优化上述方法,其思想是遍历给定的字符串并独立地找到每个位置的变化。请按照以下步骤解决问题:

  1. [0,(N / 2)– 1]范围内遍历给定的字符串。
  2. 对于索引i处的每个字符,找到索引i(N – i – 1)处的字符之间的绝对差。
  3. 可能存在两个差异,即,当i处的字符递增到索引(N – i – 1)处的字符时,以及该索引处的字符递减时的区别。
  4. 取两者中的最小值,并将其添加到结果中。
  5. 完成上述步骤后,打印计算出的最低费用。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to convert
// th given string into palindrome
int minOperations(string s)
{
    int len = s.length();
    int result = 0;
 
    // Iterate till half of the string
    for (int i = 0; i < len / 2; i++) {
 
        // Find the absolute difference
        // between the characters
        int D1 = max(s[i], s[len - 1 - i])
                - min(s[i], s[len - 1 - i]);
 
        int D2 = 26 - D1;
 
        // Adding the minimum difference
        // of the two result
        result += min(D1, D2);
    }
 
    // Return the result
    return result;
}
 
// Driver Code
int main()
{
    // Given string
    string s = "abccdb";
 
    // Function Call
    cout << minOperations(s);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the minimum number
// of operations required to convert
// th given string into palindrome
public static int minOperations(String s)
{
    int len = s.length();
    int result = 0;
 
    // Iterate till half of the string
    for(int i = 0; i < len / 2; i++)
    {
         
        // Find the absolute difference
        // between the characters
        int D1 = Math.max(s.charAt(i),
                          s.charAt(len - 1 - i)) -
                 Math.min(s.charAt(i),
                          s.charAt(len - 1 - i));
 
        int D2 = 26 - D1;
 
        // Adding the minimum difference
        // of the two result
        result += Math.min(D1, D2);
    }
 
    // Return the result
    return result;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given string
    String s = "abccdb";
 
    // Function call
    System.out.println(minOperations(s));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to convert
# th given string into palindrome
def minOperations(s):
 
    length = len(s)
    result = 0
 
    # Iterate till half of the string
    for i in range(length // 2):
 
        # Find the absolute difference
        # between the characters
        D1 = (ord(max(s[i], s[length - 1 - i])) -
              ord(min(s[i], s[length - 1 - i])))
         
        D2 = 26 - D1
 
        # Adding the minimum difference
        # of the two result
        result += min(D1, D2)
 
    # Return the result
    return result
 
# Driver Code
 
# Given string
s = "abccdb"
 
# Function call
print(minOperations(s))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the minimum number
// of operations required to convert
// th given string into palindrome
public static int minOperations(String s)
{
    int len = s.Length;
    int result = 0;
 
    // Iterate till half of the string
    for(int i = 0; i < len / 2; i++)
    {
         
        // Find the absolute difference
        // between the characters
        int D1 = Math.Max(s[i],
                          s[len - 1 - i]) -
                 Math.Min(s[i],
                          s[len - 1 - i]);
 
        int D2 = 26 - D1;
 
        // Adding the minimum difference
        // of the two result
        result += Math.Min(D1, D2);
    }
 
    // Return the result
    return result;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given string
    String s = "abccdb";
 
    // Function call
    Console.WriteLine(minOperations(s));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

3

时间复杂度: O(N)
辅助空间: O(N)