java - Balance Check if brackets are closed in a string using stacks -


i writing program check see if sets of brackets closed in user-inputed string, confused how because need use stacks accomplish it.

my idea add brackets stack if exist, if closing bracket shows up, pop off top 2 characters stack , if second popped off character matches first (as in type of bracket matches , both opening , closing bracket) line balanced. however, need have able work strings multiple brackets , characters in them, example:

 wfsfs[{{{(s;dkls(dslkf)s;dlkf}]}]}}}sd 

i confused how make work using stacks! ideas?

here code came far doesn't work multiple brackets

    (int = 0; < x.length(); i++){         if (x.charat(i) == '('){             stack.push('(');              }         if (x.charat(i) == '['){             stack.push('(');             }         if (x.charat(i) == '{'){             stack.push('(');             }         if (x.charat(i) == ')'){             stack.pop();              if (stack.empty()){                 return true;                  }             if (stack.pop() != ')'){                 return true;                 }             }         if (x.charat(i) == ']'){             stack.pop();              if (stack.empty()){                 return true;                  }             if (stack.pop() != ']'){                 return true;                 }             }         if (x.charat(i) == '}'){             stack.pop();              if (stack.empty()){                 return true;                  }             if (stack.pop() != '}'){                 return true;                 }             }      }      return false; }   } 

edit: "x" inputed sentence

so, thought. push open bracket onto stack (and push when have open bracket), pop whenever have closing bracket. if unable pop because there nothing on stack, test fails. if end , there's on stack, test fails.

this gets harder if have paired (i.e. {{}{}{}} failure not success), track once start popping if push again fail.

edit: if have match 1 of 3 kinds of "brackets" (technically have brackets, curly brackets, , parentheses) have 3 different stacks, or validate peek top "bracket" on stack matches closing one.

edit2: show pseudocode example:

i have string: "[[]]"

scanning through string, see first character open bracket [ pop onto stack, making stack 1 item large '[' on it.

my next character open bracket, pop onto stack well, meaning have 2 items on stack, '[' , '['

the third character closed bracket. peek on top of stack, , see matches open bracket there, pop open bracket off stack, leaving me 1 open bracket on stack '['

my fourth character closed bracket. peek on top of stack , see matches open bracket on stack. pop bracket off stack, leaving me empty stack.

i end string, see have empty stack, passes.

edit3: example different brackets.

say have string : "[{(){}}]"

my first character '[' pop onto stack

my second character '{' pop onto stack

my third character '(' pop onto stack

my fourth cahracter ')', check top of stack peek. matching paren '(' pop off stack , move on.

my fifth character '{' pop on stack.

my sixth character '}' peek @ top of stack, see matching '{' , pop off stack.

my seventh character '}' peek @ top of stack, , see matching '{' pop off stack.

my 8 character ']' peek @ top of stack , see matching ']' pop off stack.

i reach end of string , have nothing left on stack, good, , case passes.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -