Replace even-indexed characters of minimum number of substrings to convert a string to another
Last Updated :
23 Aug, 2021
Given two strings, str1 and str2 of length N, the task is to convert the string str1 to string str2 by selecting a substring and replacing all characters present at even indices of the substring by any possible characters, even number of times.
Examples:
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.
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Initialize a variable, say cntOp, to store the minimum count of given operations required to convert the string str1 to str2.
- Iterate over the characters of the string. For every ith index, check if str1[i] and str2[i] are same or not. If found to be different, then find the longest substring possible that contains different characters at even indices in both the strings. Replace even indexed characters of that substring of str1 and increment the value of cntOp by 1.
- Finally, print the value of cntOp.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperationsReq(string str1, string str2)
{
int N = str1.length();
int cntOp = 0;
for ( int i = 0; i < N; i++) {
if (str1[i] == str2[i]) {
continue ;
}
int ptr = i;
while (ptr < N && str1[ptr] != str2[ptr]) {
str1[ptr] = str2[ptr];
ptr += 2;
}
cntOp++;
}
return cntOp;
}
int main()
{
string str1 = "abcdef" ;
string str2 = "ffffff" ;
cout << minOperationsReq(str1, str2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int min_Operations(String str1,
String str2)
{
int N = str1.length();
char [] str = str1.toCharArray();
int cntOp = 0 ;
for ( int i = 0 ; i < N; i++) {
if (str[i] == str2.charAt(i)) {
continue ;
}
int ptr = i;
while (ptr < N && str[ptr] != str2.charAt(ptr)) {
str[ptr] = str2.charAt(ptr);
ptr += 2 ;
}
cntOp++;
}
return cntOp;
}
public static void main(String[] args)
{
String str1 = "abcdef" ;
String str2 = "ffffff" ;
System.out.println(
min_Operations(str1, str2));
}
}
|
Python3
def minOperationsReq(str11, str22):
str1 = list (str11)
str2 = list (str22)
N = len (str1)
cntOp = 0
for i in range (N):
if (str1[i] = = str2[i]):
continue
ptr = i
while (ptr < N and str1[ptr] ! = str2[ptr]):
str1[ptr] = str2[ptr]
ptr + = 2
cntOp + = 1
return cntOp
str1 = "abcdef"
str2 = "ffffff"
print (minOperationsReq(str1, str2))
|
C#
using System;
class GFG
{
static int min_Operations(String str1,
String str2)
{
int N = str1.Length;
char [] str = str1.ToCharArray();
int cntOp = 0;
for ( int i = 0; i < N; i++)
{
if (str[i] == str2[i])
{
continue ;
}
int ptr = i;
while (ptr < N && str[ptr] != str2[ptr])
{
str[ptr] = str2[ptr];
ptr += 2;
}
cntOp++;
}
return cntOp;
}
public static void Main(String[] args)
{
String str1 = "abcdef" ;
String str2 = "ffffff" ;
Console.WriteLine(
min_Operations(str1, str2));
}
}
|
Javascript
<script>
function min_Operations( str1, str2)
{
var N = str1.length;
var cntOp = 0;
for ( var i = 0; i < N; i++) {
if (str1.charCodeAt(i)== str2.charCodeAt(i))
{
continue ;
}
var ptr = i;
while (ptr < N && str1[ptr] != str2[ptr]) {
str1 = str1.substring(0, ptr) + str2[ptr]
+ str1.substring(ptr + 1);
ptr += 2;
}
cntOp++;
}
return cntOp;
}
str1 = "abcdef" ;
str2 = "ffffff" ;
document.write(min_Operations(str1, str2));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...