Replace even | C ++ 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 string str1 to str2 ; Stores length of str1 ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the strings are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }