Convert given string to another by minimum replacements of subsequences by its smallest character | Function to return the minimum number of operation ; Storing data ; Initialize both arrays ; Stores the index of character ; Filling str1array , convChar and hashmap convertMap . ; Not possible to convert ; Calculate result Initializing return values ; Iterating the character from the end ; Increment the number of operations ; Not possible to convert ; To check whether the final element has been added in set S or not . ; Check if v1 [ j ] is present in hashmap or not ; Already converted then then continue ; Not possible to convert ; Print the result ; Given strings ; Function call
def transformString ( str1 , str2 ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE convChar = [ ] NEW_LINE str1array = [ ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT convChar . append ( [ ] ) NEW_LINE str1array . append ( [ ] ) NEW_LINE DEDENT convertMap = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT str1array [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( str1 [ i ] < str2 [ i ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT elif ( str1 [ i ] == str2 [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT convChar [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE convertMap [ i ] = str2 [ i ] NEW_LINE DEDENT DEDENT ret = 0 NEW_LINE retv = [ ] NEW_LINE for i in range ( 25 , - 1 , - 1 ) : NEW_LINE INDENT v = convChar [ i ] NEW_LINE if ( len ( v ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ret += 1 ; NEW_LINE v1 = str1array [ i ] NEW_LINE if ( len ( v1 ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT isScompleted = False NEW_LINE for j in range ( len ( v1 ) ) : NEW_LINE INDENT if ( v1 [ j ] in convertMap ) : NEW_LINE INDENT a = v1 [ j ] NEW_LINE if ( a > i + ord ( ' a ' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( v1 [ j ] ) NEW_LINE isScompleted = True NEW_LINE retv . append ( v ) NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT v . append ( v1 [ j ] ) NEW_LINE isScompleted = True NEW_LINE retv . append ( v ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( isScompleted == False ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( ret ) NEW_LINE DEDENT A = " abcab " NEW_LINE B = " aabab " NEW_LINE transformString ( A , B ) NEW_LINE