📌  相关文章
📜  将字符替换为与其最近的字母表的次数最少,以使字符串回文

📅  最后修改于: 2021-09-06 06:24:05             🧑  作者: 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live