Assuming we do not want to remove
brackets from cases like (a+b)+c,
(because strictly speaking, the brackets
here are not required), one algorithm to
remove brackets for given above cases can be:
1) Scan through the string.
2) Increment a counter when '(' is
encountered and decrement it on ')'
3) When a '(' is obtained just after
another '(', push the value of counter
onto a stack.
4) When a ')' is obtained just after
another ')', pop the stack and see if it
matches the counter now.
If (curr_counter+2 > popped_value) {
// This is a case like: a *((b +*(c+d))+e)
// where )) are required.
push (popped_value)
} else
If (curr_counter+2 == popped_value) {
// here is an extra brackets pair, remove it
} else
If (curr_counter+2 < popped_value) {
// This is an opposite case of #1 where (( are
// required and not extraneous
// pop another value from stack and repeat the above checks
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.