human
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"
Solution in Python:
assistant
#Here is code to convert given string to another by minimum replacements of subsequences by its smallest character and the other steps required.
 def transformString ( str1 , str2 ) :
	N = len ( str1 )
	convChar = [ ]
	str1array = [ ]
	for i in range ( 26 ) :
		convChar . append ( [ ] )
		str1array . append ( [ ] )
	convertMap = { }
	for i in range ( N ) :
		str1array [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] . append ( i )
	for i in range ( N ) :
		if ( str1 [ i ] < str2 [ i ] ) :
			print ( - 1 )
			return
		elif ( str1 [ i ] == str2 [ i ] ) :
			continue
		else :
			convChar [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] . append ( i )
			convertMap [ i ] = str2 [ i ]
	ret = 0
	retv = [ ]
	for i in range ( 25 , - 1 , - 1 ) :
		v = convChar [ i ]
		if ( len ( v ) == 0 ) :
			continue
		ret += 1 ;
		v1 = str1array [ i ]
		if ( len ( v1 ) == 0 ) :
			print ( - 1 )
			return
		isScompleted = False
		for j in range ( len ( v1 ) ) :
			if ( v1 [ j ] in convertMap ) :
				a = v1 [ j ]
				if ( a > i + ord ( ' a ' ) ) :
					continue
				else :
					v . append ( v1 [ j ] )
					isScompleted = True
					retv . append ( v )
					break
			else :
				v . append ( v1 [ j ] )
				isScompleted = True
				retv . append ( v )
				break
		if ( isScompleted == False ) :
			print ( - 1 )
			return
	print ( ret )
A = " abcab "
B = " aabab "
transformString ( A , B )