human
Maximize cost of removing all occurrences of substrings " ab " and " ba " | Function to find the maximum cost of removing substrings " ab " and " ba " from S.
MaxStr is the substring char array with larger cost.
MinStr is the substring char array with smaller cost.
.
Denotes larger point.
Denotes smaller point.
Stores cost scored.
Stack to keep track of characters.
Traverse the string.
If the substring is maxstr.
Pop from the stack.
Add maxp to cost.
Push the character to the stack.
Remaining string after removing maxstr.
Find remaining string.
Reversing the string retrieved from the stack.
Removing all occurences of minstr.
If the substring is minstr.
Pop from the stack.
Add minp to the cost.
Otherwise.
Return the maximum cost.
Driver Code.
Input String.
Costs"
How can the above be solved in Python?
 What is a code to maximize cost of removing all occurrences of substrings " ab " and " ba " and the other steps required?
assistant
#Here is code to maximize cost of removing all occurrences of substrings " ab " and " ba " and the other steps required.
 def MaxCollection ( S , P , Q ) :
	maxstr = [ i for i in ( " ab " if P >= Q else " ba " ) ]
	minstr = [ i for i in ( " ba " if P >= Q else " ab " ) ]
	maxp = max ( P , Q )
	minp = min ( P , Q )
	cost = 0
	stack1 = [ ]
	s = [ i for i in S ]
	for ch in s :
		if ( len ( stack1 ) > 0 and ( stack1 [ - 1 ] == maxstr [ 0 ] and ch == maxstr [ 1 ] ) ) :
			del stack1 [ - 1 ]
			cost += maxp
		else :
			stack1 . append ( ch )
	sb = " "
	while ( len ( stack1 ) > 0 ) :
		sb += stack1 [ - 1 ]
		del stack1 [ - 1 ]
	sb = sb [ : : - 1 ]
	remstr = sb
	for ch in remstr :
		if ( len ( stack1 ) > 0 and ( stack1 [ - 1 ] == minstr [ 0 ] and ch == minstr [ 1 ] ) ) :
			del stack1 [ - 1 ]
			cost += minp
		else :
			stack1 . append ( ch )
	return cost
if __name__ == ' _ _ main _ _ ' :
	S = " cbbaabbaab "
	P = 6 ;
	Q = 4 ;
	print ( MaxCollection ( S , P , Q ) ) ;