Replace even | Java 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 ; Convert str1 to character array ; 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 string are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
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 ) ) ; } }