通过将前缀加1来最小化使字符串回文的运算次数
在这个问题中,我们将计算通过增加给定字符串的前缀字符所需的运算次数。
我们将使用字符差异来计算使字符串回文所需的最小运算次数。
问题陈述
我们得到一个包含数字字符的字符串nums。我们需要计算将字符串转换为回文所需的最小运算次数。
在一个操作中,我们可以选择字符串的任何前缀并将所有前缀字符加1。
示例
输入
nums = "22434"
输出
2
解释
首先,我们可以选择前2个字符并递增所有字符。因此,字符串变为33434。
之后,我们可以选择'3'前缀,字符串变为43434,这是一个回文串。
输入
nums = '151'
输出
0
解释 - 字符串已经是回文串。所以它输出0。
输入
nums = "32102"
输出
-1
解释 - 不可能通过递增前缀值将字符串转换为回文串。
方法1
如果字符串满足以下两个条件,我们可以根据问题陈述使字符串回文。
将字符串分成两等份后,第一部分的数字应该小于第二部分。
在左半部分,起始字符应该大于结束字符,因为我们需要选择任何前缀并将每个字符加1。
算法
步骤1 − 将q初始化为len - 1,将p初始化为0,因为我们将其用作索引指针。将maxOps初始化为最大整数值以存储最小操作数,并将'curr'初始化为0以存储最大差值。
步骤2 − 开始遍历字符串,直到q > p。
步骤3 − 如果索引q处的字符小于索引p处的字符,则返回-1,因为将字符串转换为回文是不可能的。
步骤4 − 将索引q和p处的字符的ASCII值之差存储到'diff'变量中。
步骤5 − 将'curr'和'diff'的最大值存储在'curr'变量中。
步骤6 − 如果'maxOps'值小于'diff',则返回-1。
步骤7 − 使用'diff'值更新'maxOps'。
步骤8 − 将p加1,将q减1。
步骤9 − 返回'curr'值。
示例
#include <stdio.h> #include <string.h> #include <limits.h> int makePalindrome(char alpha[], int len) { int q = len - 1; int p = 0; int maxOpes = INT_MAX; int curr = 0; // Traverse from both ends while (q > p) { // It is not possible to make the string palindromic if (alpha[q] < alpha[p]) { return -1; } // Get character difference int diff = alpha[q] - alpha[p]; // Get the maximum current difference curr = (curr > diff) ? curr : diff; // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } int main() { char nums[] = "22434"; int len = strlen(nums); printf("The number of minimum operations required to make the string palindromic is %d\n", makePalindrome(nums, len)); return 0; }
输出
The number of minimum operations required to make the string palindromic is 2
#include <bits/stdc++.h> using namespace std; int makePalindrome(string alpha, int len) { int q = len - 1; int p = 0; int maxOpes = INT_MAX; int curr = 0; // Travere from both ends while (q > p) { // It is not possible to make string palindromic if (alpha[q] < alpha[p]) { return -1; } // Get character difference int diff = alpha[q] - alpha[p]; // Get the maximum current difference curr = max(curr, diff); // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } int main() { string nums = "22434"; int len = nums.length(); cout << "The number of minimum operations required to make string palindromic is " << makePalindrome(nums, len); return 0; }
输出
The number of minimum operations required to make string palindromic is 2
public class Main { public static int makePalindrome(String alpha) { int len = alpha.length(); int q = len - 1; int p = 0; int maxOpes = Integer.MAX_VALUE; int curr = 0; // Traverse from both ends while (q > p) { // It is not possible to make the string palindromic if (alpha.charAt(q) < alpha.charAt(p)) { return -1; } // Get character difference int diff = alpha.charAt(q) - alpha.charAt(p); // Get the maximum current difference curr = Math.max(curr, diff); // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } public static void main(String[] args) { String nums = "22434"; int len = nums.length(); System.out.println("The number of minimum operations required to make the string palindromic is " + makePalindrome(nums)); } }
输出
The number of minimum operations required to make the string palindromic is 2
def make_palindrome(alpha): q = len(alpha) - 1 p = 0 max_opes = float('inf') curr = 0 # Traverse from both ends while q > p: # It is not possible to make the string palindromic if alpha[q] < alpha[p]: return -1 # Get character difference diff = ord(alpha[q]) - ord(alpha[p]) # Get the maximum current difference curr = max(curr, diff) # At the center side difference should be less than the characters at the end if max_opes < diff: return -1 max_opes = diff p += 1 q -= 1 return curr nums = "22434" print(f"The number of minimum operations required to make the string palindromic is {make_palindrome(nums)}")
输出
The number of minimum operations required to make the string palindromic is 2
时间复杂度 − O(N),用于遍历字符串。
空间复杂度 − O(1),因为我们不使用任何额外的空间。
在解决方案中,我们检查从开头到中心的差异,如果任何中心侧字符具有更高的差异,则返回-1。程序员可以尝试从中心遍历字符串,并检查起始侧的较高差异。