human
Replace two substrings ( of a string ) with each other | Function to return the resultant string.
Iterate through all positions i.
Current sub - string of length = len ( A ) = len ( B ).
If current sub - string gets equal to A or B.
Update S after replacing A.
Update S after replacing B.
Return the updated string.
Driver code"
How can the above be solved in Python?
 ... Can you provide a code to replace two substrings ( of a string ) with each other and the other steps required?
assistant
#Here is code to replace two substrings ( of a string ) with each other and the other steps required.
 def updateString ( S , A , B ) :
	l = len ( A )
	i = 0
	while i + l <= len ( S ) :
		curr = S [ i : i + l ]
		if curr == A :
			new_string = S [ 0 : i ] + B + S [ i + l : len ( S ) ]
			S = new_string
			i += l - 1
		else :
			new_string = S [ 0 : i ] + A + S [ i + l : len ( S ) ]
			S = new_string
			i += l - 1
		i += 1
	return S
if __name__ == " _ _ main _ _ " :
	S = " aab "
	A = " aa "
	B = " bb "
	print ( updateString ( S , A , B ) )