VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-for-balanced-parenthesis-without-using-stack/

⇱ Check for balanced parenthesis without using stack - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check for balanced parenthesis without using stack

Last Updated : 11 Jul, 2025

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“, ”}”, ”(“, ”)”, ”[“, ”]” are correct in exp. 

Examples: 

Input : exp = “[()]{}{[()()]()}”
Output : true

Input : exp = “[(])”
Output : false

We have discussed a stack based solution. Here we are not allowed to use the stack. Looks like this problem cannot be solved without extra space (please see comments at the end). We use recursion to solve the problem. 

Follow the steps below to solve the problem:

  • Define a function findClosing(char c) which takes a character c representing an opening bracket and returns its corresponding closing bracket.
  • Define a function check(char expr[], int n) which takes an array of characters expr representing the expression and its length n, and returns a boolean value indicating whether the expression is balanced or not.
  • Implement the following logic inside the check() function:
    • If n is 0, return true (empty expression is balanced).
    • If n is 1, return false (single bracket is not balanced).
    • If the first character in expr is a closing bracket, return false (unbalanced expression).
    • Find the corresponding closing bracket for the first opening bracket in expr using the findClosing() function.
    • Search for the closing bracket in the rest of the expression (expr[1] to expr[n-1]) and ensure that it appears after the opening bracket. If there are nested brackets, recursively check the sub-expression between the opening and closing brackets.
    • If the closing bracket was not found or was in the wrong order, return false. Otherwise, remove the matched opening and closing brackets and recursively check the remaining expression.
  • In the main function, call check() with the given expression and its length and print "Balanced" or "Not Balanced" based on the returned boolean value.

Below is the implementation of the above algorithm:  


Output
Not Balanced

Time Complexity:
The given implementation of the balanced parenthesis check algorithm uses recursion. For each recursive call, we iterate over the input expression once. Thus, the time complexity can be expressed as O(n^2), where n is the length of the input expression.

Space Complexity:
The algorithm uses recursion, which creates a new stack frame for each recursive call. The space occupied by the stack frames is proportional to the maximum depth of the recursion tree. The maximum depth of the recursion tree in the worst case is n/2, where n is the length of the input expression. Therefore, the space complexity of the algorithm can be expressed as O(n).

The above solution is very inefficient compared to the stack-based solution. This seems to only useful for recursion practice problems. 

Comment