给定两个长度为N 的字符串str1和str2 ,任务是通过选择一个子字符串并将出现在子字符串偶数索引处的所有字符替换为任何可能的偶数次的字符,从而将字符串str1转换为字符串str2 。
例子:
Input: str1 = “abcdef”, str2 = “ffffff”
Output: 2
Explanation:
Selecting the substring {str1[0], …, str[4]} and replacing all the even indices of the substring by ‘f’ modifies str1 to “fbfdff”.
Selecting the substring {str1[1], …, str[3]} and replacing all the even indices of the substring by ‘f’ modifies str1 to “ffffff”, which is the same as str2.
Therefore, the required output is 2.
Input: str1 = “rtrtyy”, str2 = “wtwtzy”
Output: 1
Explanation:
Selecting the substring {str1[0], …, str[4]} and replacing str1[0] by ‘w’, str1[[2] by ‘w’ and str[4] by ‘t’ modifies str1 to “wtwtzy”, which is same as str2. Therefore, the required output is 1.
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 初始化一个变量,比如cntOp ,以存储将字符串str1转换为str2所需的给定操作的最小计数。
- 遍历字符串的字符。对于每个第i个索引,检查str1[i]和str2[i]是否相同。如果发现不同,则找到可能在两个字符串的偶数索引处包含不同字符的最长子字符串。替换str1的该子字符串的偶数索引字符并将cntOp的值增加1 。
- 最后,打印cntOp的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to count the minimum number of
// substrings of str1 such that replacing
// even-indexed characters of those substrings
// converts the string str1 to str2
int minOperationsReq(string str1, string str2)
{
// Stores length of str1
int N = str1.length();
// Stores minimum count of operations
// to convert str1 to str2
int cntOp = 0;
// Traverse both the given string
for (int i = 0; i < N; i++) {
// If current character in
// both the strings are equal
if (str1[i] == str2[i]) {
continue;
}
// Stores current index
// of the string
int ptr = i;
// If current character in both
// the strings are not equal
while (ptr < N && str1[ptr] != str2[ptr]) {
// Replace str1[ptr]
// by str2[ptr]
str1[ptr] = str2[ptr];
// Update ptr
ptr += 2;
}
// Update cntOp
cntOp++;
}
return cntOp;
}
// Driver Code
int main()
{
string str1 = "abcdef";
string str2 = "ffffff";
cout << minOperationsReq(str1, str2);
return 0;
}
Java
// Java porgram to implement
// the above approach
import java.io.*;
class GFG {
// Function to count the minimum number of
// substrings of str1 such that replacing
// even-indexed characters of those substrings
// converts the string str1 to str2
static int min_Operations(String str1,
String str2)
{
// Stores length of str1
int N = str1.length();
// Convert str1 to character array
char[] str = str1.toCharArray();
// Stores minimum count of operations
// to convert str1 to str2
int cntOp = 0;
// Traverse both the given string
for (int i = 0; i < N; i++) {
// If current character in both
// the strings are equal
if (str[i] == str2.charAt(i)) {
continue;
}
// Stores current index
// of the string
int ptr = i;
// If current character in both the
// string are not equal
while (ptr < N && str[ptr] != str2.charAt(ptr)) {
// Replace str1[ptr]
// by str2[ptr]
str[ptr] = str2.charAt(ptr);
// Update ptr
ptr += 2;
}
// Update cntOp
cntOp++;
}
return cntOp;
}
// Driver Code
public static void main(String[] args)
{
String str1 = "abcdef";
String str2 = "ffffff";
System.out.println(
min_Operations(str1, str2));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to count the minimum number of
# substrings of str1 such that replacing
# even-indexed characters of those substrings
# converts the str1 to str2
def minOperationsReq(str11, str22):
str1 = list(str11)
str2 = list(str22)
# Stores length of str1
N = len(str1)
# Stores minimum count of operations
# to convert str1 to str2
cntOp = 0
# Traverse both the given string
for i in range(N):
# If current character in
# both the strings are equal
if (str1[i] == str2[i]):
continue
# Stores current index
# of the string
ptr = i
# If current character in both
# the strings are not equal
while (ptr < N and str1[ptr] != str2[ptr]):
# Replace str1[ptr]
# by str2[ptr]
str1[ptr] = str2[ptr]
# Update ptr
ptr += 2
# Update cntOp
cntOp += 1
return cntOp
# Driver Code
str1 = "abcdef"
str2 = "ffffff"
print(minOperationsReq(str1, str2))
# This code is contributed by code_hunt
C#
// C# porgram to implement
// the above approach
using System;
class GFG
{
// Function to count the minimum number of
// substrings of str1 such that replacing
// even-indexed characters of those substrings
// converts the string str1 to str2
static int min_Operations(String str1,
String str2)
{
// Stores length of str1
int N = str1.Length;
// Convert str1 to character array
char[] str = str1.ToCharArray();
// Stores minimum count of operations
// to convert str1 to str2
int cntOp = 0;
// Traverse both the given string
for (int i = 0; i < N; i++)
{
// If current character in both
// the strings are equal
if (str[i] == str2[i])
{
continue;
}
// Stores current index
// of the string
int ptr = i;
// If current character in both the
// string are not equal
while (ptr < N && str[ptr] != str2[ptr])
{
// Replace str1[ptr]
// by str2[ptr]
str[ptr] = str2[ptr];
// Update ptr
ptr += 2;
}
// Update cntOp
cntOp++;
}
return cntOp;
}
// Driver Code
public static void Main(String[] args)
{
String str1 = "abcdef";
String str2 = "ffffff";
Console.WriteLine(
min_Operations(str1, str2));
}
}
// This code contributed by gauravrajput1
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live