Use stack to push '( initially. if ')' is found remove its pair
'('.finally if the stack is empty then the expression is correct!
On Jun 10, 9:45 pm, jalaj jaiswal jalaj.jaiswa...@gmail.com wrote:
give an algo which reads an paranthesised infix expression and check
well-formdness of
BALARUKESH, In Rohit's algorithm there was one more condition that sum
should never be negative. I think you missed that point. The algorithm seems
to be fine at least for correct ordering of paranthesis. didn't check for
correctness of expression. :P
--
You received this message because you are
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,
@Rohit
Consider : (a+)b
The above is not well formed! :)
On 11 June 2010 11:58, Rohit Saraf rohit.kumar.sa...@gmail.com wrote:
@BALARUKESH :
What are you saying !!
and Why would this not work
As you start you get sum -1 at start itself. Hence you quit.
The sum should be 0 always and 0 at
On 2010-6-11 13:39, BALARUKESH wrote:
The increment and decrement method wont work correctly for all
cases
Ex:-
))a+b((
It works. In this example, the first ')' lead to -1 which indicate the
expression is not well formed.
This is not a well formed parenthesis... But satisfies the
@balarukesh thanks
On Fri, Jun 11, 2010 at 11:58 AM, Rohit Saraf
rohit.kumar.sa...@gmail.comwrote:
@BALARUKESH :
What are you saying !!
and Why would this not work
As you start you get sum -1 at start itself. Hence you quit.
The sum should be 0 always and 0 at last
@vivek:
read again:
) = -1
( = +1
Keep a sum of all these as u iterate.
That should never be negative
Plus check for these types (if you need correct arithmetic expressions as
well
*some operator followed by )*
* or / after a (
()
--
I missed out the condition that it should never be negative... Sorry for the
comment... Thanks for correcting...
--
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
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