给定长度为N的字符串S和表示指向字符串的第P个索引的指针的整数P ,任务是通过执行以下操作找到将字符串转换为回文的最小成本:
- 指针P可以从索引i移动到索引j并且所需的成本是|i – j|其中0 ≤ i < N和0 ≤ j < N 。
- 更改第 P个索引处的字符的成本是1 。
例子:
Input: S = “saad”, P = 1
Output: 2
Explanation:
Initially the pointer is at index 1.
Step 1: Move pointer to index 0. Therefore, cost = 1 – 0 = 1.
Step 2: Change character s to d. Therefore, cost = 1 + 1 = 2 and S = “daad”
Hence, the cost is 2.
Input: S = “bass”, P = 3
Output: 3
Explanation:
Initially the pointer is at index 3.
Step 1: Change character at index P = 3 to b. Therefore, cost = 1 and S = “basb”.
Step 2: Move pointer to index 2. Therefore, cost = 1 + 1 = 2.
Step 3: Change character at index P = 2 to a. Therefore, cost = 3 and S = “baab”
Hence, the cost is 3.
方法:我们的想法是找到的第一个和最后一个字符是在字符串的前半部分被改变的需求,当指针在上半年或字符串逆向如果指针是在下半年,并调整相应的指针.请按照以下步骤解决问题:
- 如果指针在后半部分,则反转字符串并将指针更改为(N – 1 – P) 。
- 现在,在字符串的前半部分中找到需要更改的最远字符的左侧和右侧位置。让它们是l和r 。
- 找出更改字符的总成本,即前半部分的总字符数,使得S[i] != s[N – i – 1]其中0 < i < N/2 。
- 改变字符的最小距离为min(2*(P – l) + r – P, 2*(r – P) + P – l) 。
- 将答案打印为更改字符的成本和最小旅行距离的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost to
// convert the string into a palindrome
int findMinCost(string str, int pos)
{
// Length of the string
int n = str.length();
// If iointer is in the second half
if (pos >= n / 2) {
// Reverse the string
reverse(str.begin(), str.end());
pos = n - pos - 1;
}
int left, right;
// Pointing to intial position
left = right = pos;
// Find the farthest index needed to
// change on left side
for (int i = pos; i >= 0; --i) {
if (str[i] != str[n - i - 1]) {
left = i;
}
}
// Find the farthest index needed to
// change on right side
for (int i = pos; i < n / 2; ++i) {
if (str[i] != str[n - i - 1]) {
right = i;
}
}
int ans = 0;
for (int i = left; i <= right; ++i) {
// Changing the variable to make
// string palindrome
if (str[i] != str[n - i - 1])
ans += 1;
}
// min distance to travel to make
// string palindrome
int dis = min((2 * (pos - left)
+ (right - pos)),
(2 * (right - pos)
+ (pos - left)));
// Total cost
ans = ans + dis;
// Return the minimum cost
return ans;
}
// Driver Code
int main()
{
// Given string S
string S = "bass";
// Given pointer P
int P = 3;
// Function Call
cout << findMinCost(S, P);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum cost to
// convert the string into a palindrome
static int findMinCost(String str, int pos)
{
// Length of the string
int n = str.length();
StringBuilder input1 = new StringBuilder();
input1.append(str);
// If iointer is in the second half
if (pos >= n / 2)
{
// Reverse the string
input1 = input1.reverse();
pos = n - pos - 1;
}
int left, right;
// Pointing to intial position
left = right = pos;
// Find the farthest index needed to
// change on left side
for(int i = pos; i >= 0; --i)
{
if (input1.charAt(i) !=
input1.charAt(n - i - 1))
{
left = i;
}
}
// Find the farthest index needed to
// change on right side
for(int i = pos; i < n / 2; ++i)
{
if (input1.charAt(i) !=
input1.charAt(n - i - 1))
{
right = i;
}
}
int ans = 0;
for(int i = left; i <= right; ++i)
{
// Changing the variable to make
// string palindrome
if (input1.charAt(i) !=
input1.charAt(n - i - 1))
ans += 1;
}
// min distance to travel to make
// string palindrome
int dis = Math.min((2 * (pos - left) +
(right - pos)),
(2 * (right - pos) +
(pos - left)));
// Total cost
ans = ans + dis;
// Return the minimum cost
return ans;
}
// Driver Code
public static void main(String args[])
{
// Given string S
String S = "bass";
// Given pointer P
int P = 3;
// Function Call
System.out.println(findMinCost(S, P));
}
}
// This code is contributed by bgangwar59
Python3
# Python3 program for the above approach
# Function to find the minimum cost to
# convert the into a palindrome
def findMinCost(strr, pos):
# Length of the strring
n = len(strr)
# If iointer is in the second half
if (pos >= n / 2):
# Reverse the strring
strr = strr[::-1]
pos = n - pos - 1
left, right = pos, pos
# Pointing to intial position
# left = right = pos
# Find the farthest index needed
# to change on left side
for i in range(pos, -1, -1):
if (strr[i] != strr[n - i - 1]):
left = i
# Find the farthest index needed
# to change on right side
for i in range(pos, n // 2):
if (strr[i] != strr[n - i - 1]):
right = i
ans = 0
for i in range(left, right + 1):
# Changing the variable to make
# palindrome
if (strr[i] != strr[n - i - 1]):
ans += 1
# min distance to travel to make
# palindrome
dis = (min((2 * (pos - left) +
(right - pos)),
(2 * (right - pos) +
(pos - left))))
# Total cost
ans = ans + dis
# Return the minimum cost
return ans
# Driver Code
if __name__ == '__main__':
# Given S
S = "bass"
# Given pointer P
P = 3
# Function Call
print(findMinCost(S, P))
# This code is contributed by mohit kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to find the minimum
// cost to convert the string
// into a palindrome
static int findMinCost(string str,
int pos)
{
// Length of the string
int n = str.Length;
char[] charArray =
str.ToCharArray();
// If iointer is in the
// second half
if (pos >= n / 2)
{
// Reverse the string
Array.Reverse(charArray);
pos = n - pos - 1;
}
int left, right;
// Pointing to intial position
left = right = pos;
// Find the farthest index
// needed to change on
// left side
for(int i = pos; i >= 0; --i)
{
if (charArray[i] !=
charArray[n - i - 1])
{
left = i;
}
}
// Find the farthest index
// needed to change on right
// side
for(int i = pos; i < n / 2; ++i)
{
if (charArray[i] !=
charArray[n - i - 1])
{
right = i;
}
}
int ans = 0;
for(int i = left; i <= right; ++i)
{
// Changing the variable to make
// string palindrome
if (charArray[i]!=
charArray[n - i - 1])
ans += 1;
}
// min distance to travel to make
// string palindrome
int dis = Math.Min((2 * (pos - left) +
(right - pos)),
(2 * (right - pos) +
(pos - left)));
// Total cost
ans = ans + dis;
// Return the minimum cost
return ans;
}
// Driver Code
public static void Main()
{
// Given string S
string S = "bass";
// Given pointer P
int P = 3;
// Function Call
Console.Write(findMinCost(S, P));
}
}
// This code is contributed by Chitranayal
Javascript
3
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live