human
Lexicographically largest string possible in one swap | Function to return the lexicographically largest string possible by swapping at most one character.
Initialize with - 1 for every character.
Keep updating the last occurrence of each character.
If a previously unvisited character occurs.
Stores the sorted string.
Character to replace.
Find the last occurrence of this character.
Swap this with the last occurrence.
Driver code"
Solution in Python:
assistant
#Here is code to lexicographically largest string possible in one swap and the other steps required.
 def findLargest ( s ) :
	Len = len ( s )
	loccur = [ - 1 for i in range ( 26 ) ]
	for i in range ( Len - 1 , - 1 , - 1 ) :
		chI = ord ( s [ i ] ) - ord ( ' a ' )
		if ( loccur [ chI ] == - 1 ) :
			loccur [ chI ] = i
	sorted_s = sorted ( s , reverse = True )
	for i in range ( Len ) :
		if ( s [ i ] != sorted_s [ i ] ) :
			chI = ( ord ( sorted_s [ i ] ) - ord ( ' a ' ) )
			last_occ = loccur [ chI ]
			temp = list ( s )
			temp [ i ] , temp [ last_occ ] = ( temp [ last_occ ] , temp [ i ] )
			s = " " . join ( temp )
			break
	return s
s = " yrstvw "
print ( findLargest ( s ) )