In this we will see how to check if a given string take valid combination of open and close brackets i.e.

(4+{8-[22+8]*}] contains valid order of open and close brackets.

({5+8]) contains invalid combination of open and close brackets.

Bracket combinations to be used,

  • { , }
  • [,]
  • (,)

Algo Used:


  • Create a empty stack of characters
  • Traverse characters in string one by one.
    • If any open bracket is encountered, then push that in stack
    • If any type of close bracket is encountered then match it’s open bracket counterpart with top of stack.
      • If match is successful then pop character from stack.
      • If match is unsuccessful then RETURN FALSE.
  • If stack is empty RETURN TRUE;
  • If stack is not empty RETURN FALSE;

Code is as follows,

Click Here to Subscribe for more Articles / Tutorials like this.