Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread atul anand
@Terence : got it :) :) ...inc/dec wont work On Thu, Dec 22, 2011 at 10:57 AM, Terence wrote: > Then what's your output for test case "()(()"? > > > On 2011-12-22 12:45, atul anand wrote: > > i guess there is no need of stack , we can take a variable say top; > > increment top when open bracket

Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread atul anand
got it :) On Thu, Dec 22, 2011 at 10:57 AM, Terence wrote: > Then what's your output for test case "()(()"? > > > On 2011-12-22 12:45, atul anand wrote: > > i guess there is no need of stack , we can take a variable say top; > > increment top when open bracket occur "(" and decrement when close

Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread atul anand
@venky : u call this code??? On Thu, Dec 22, 2011 at 7:44 PM, venky wrote: > #include#include#define maxsize 100 > struct stack{ int A[maxsize]; int top;}; struct stack s;void > push(int > index){ s.top++;if(s.top==maxsize) { > printf("cannot be pushed"); } > else{

[algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread venky
#include#include#define maxsize 100 struct stack{ int A[maxsize]; int top;}; struct stack s;void push(int index){ s.top++;if(s.top==maxsize) { printf("cannot be pushed"); } else{ s.A[s.top]=index; }} int pop(){ int a; a=s.top;

Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread Terence
Then what's your output for test case "()(()"? On 2011-12-22 12:45, atul anand wrote: i guess there is no need of stack , we can take a variable say top; increment top when open bracket occur "(" and decrement when close bracket ")" occurs. keep track of first close bracket mismatch i.e when

Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-22 Thread Terence
And I think the comparing between first unmatched open/close bracket index is not needed. If we found an unmatched close bracket (ie. the stack is empty when encounter a close bracket), we could return current index immediately, since all open brackets before that position are matched and popp

Re: [algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-21 Thread atul anand
i guess there is no need of stack , we can take a variable say top; increment top when open bracket occur "(" and decrement when close bracket ")" occurs. keep track of first close bracket mismatch i.e when top is zero and current bracket is ")". if top!=0 report min(index,top); On Tue,

[algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-20 Thread shady
true, we have to look at the entire string to find the first mismatch, and its meaning depends on how you interpret it... either way stack will solve it :) On Dec 20, 10:32 pm, Arun Vishwanathan wrote: > @shady: I guess first mismatch means the innermost open brace that doesnt > have a close brac

[algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-20 Thread SAMMM
Continued . Forgot to metion to also keep track of the index of the Closing first bracket ( If tht closing doesn't hav matching opening bracket on it's left ) and at last check for the top of the stack(If present) and the index(if present) . And print the minimum index value of the Stack[top]

[algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-20 Thread SAMMM
This can be done using Stack . For example :- you have got the pattern :- 0 1 2 3 4 5 6 7 8 9 10 11 ( ( ) ( ( ) ( ( ) ) ( ) ... this pattern is wrong the faulty index is at index 3 . void MatchingBracket( char *str ) { while ( str[i++] != '\0' ) { if ( str[i] == '(') push(i); // If

[algogeeks] Re: Return index of first mismatch bracket "(" or ")"

2011-12-20 Thread Don
Use a stack to store the index for each open paren. When a close paren is encountered, pop one off the stack. If there is nothing in the stack, remember the index of that close paren. When you have processed the entire string, if there is anything left on the stack, the bottom item is the first unm