As you encounter the opening parentheses (,[,{ push them onto the stack.
When you encounter closing parentheses pop the top element if it is not a
matching parentheses then the expr is not valid. Finally after the input
string is scanned, the stack has to be empty else expr is invalid.

On Fri, Jun 11, 2010 at 11:09 AM, BALARUKESH <sbalarukesh1...@gmail.com>wrote:

> The increment and decrement method wont work correctly for all
> cases....
> Ex:-
> ))a+b((
> This is not a well formed parenthesis... But satisfies the algorithm.
> The better way is to use a stack... When u find a ' ( ' push it into
> the stack and when u find a ' )' pop off the top element. Finally the
> stack should be empty. If [stack has one or more ' ( ' ] or [the stack
> is empty and exp is not empty] then it is not a well formed
> parenthesis...
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algoge...@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to