human
Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | Function to maintain the case , X >= Y.
To maintain X >= Y.
Replace ' p ' to 'r.
Replace ' r ' to ' p ' ..
Function to return the maximum cost.
Stores the length of the string.
To maintain X >= Y ..
Stores the maximum cost.
Stores the count of ' p ' after removal of all " pr " substrings up to str [ i ].
Stores the count of ' r ' after removal of all " pr " substrings up to str [ i ].
Stack to maintain the order of characters after removal of substrings.
If substring " pr " is removed.
Increase cost by X.
If any substring " rp " left in the Stack.
If any substring " rp " left in the Stack.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to maximize cost obtained by removal of substrings " pr " or " rp " from a given String and the other steps required.
 def swapXandY ( str , X , Y ) :
	N = len ( str )
	X , Y = Y , X
	for i in range ( N ) :
'
	if ( str [ i ] == ' p ' ) :
		str [ i ] = ' r '
	elif ( str [ i ] == ' r ' ) :
		str [ i ] = ' p '
def maxCost ( str , X , Y ) :
	N = len ( str )
	if ( Y > X ) :
		swapXandY ( str , X , Y )
	res = 0
	countP = 0
	countR = 0
	for i in range ( N ) :
		if ( str [ i ] == ' p ' ) :
			countP += 1
		elif ( str [ i ] == ' r ' ) :
			if ( countP > 0 ) :
				countP -= 1
				res += X
			else :
				countR += 1
		else :
			res += min ( countP , countR ) * Y
			countP = 0
			countR = 0
	res += min ( countP , countR ) * Y
	return res
str = " abppprrr "
X = 5
Y = 4
print ( maxCost ( str , X , Y ) )