Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8570339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:32:09+00:00 2026-06-11T18:32:09+00:00

Possible Duplicate: Basic Recursion, Check Balanced Parenthesis I came across this problem in Algorithm

  • 0

Possible Duplicate:
Basic Recursion, Check Balanced Parenthesis

I came across this problem in Algorithm Design Manual recently, even though the stack based algorithm is very trivial I want to write a recursive algorithm for this problem, However with being a noob in recursion I am not able to come up with much, so can anyone help me with this problem?

PS I saw other posts about this question only but they are not very efficient and those who are, aint very explanotry.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:32:10+00:00Added an answer on June 11, 2026 at 6:32 pm

    Background: The problem of finding if parenthesis are balanced is actually a decision problem, and the language1 describing it is a context-free language.

    Context free grammers can be parsed using an automaton with a stack2

    So, the following iterative solution can be achieved to this problem:

    iterative(str):
      stack <- empty stack
      for each char in str:
         if char is open paranthesis: //push the paranhtesis to stack
             stack.push(char)
         else if char is close parantesis: //if close paranthesis - check if it is closing an open parenthesis
             if stack.head() == matchingParanthesis(char):
                stack.pop()
             else: //if the closing parenthesis do not close anything previously opened, return false
                 return false 
       //end of loop - check if all opened parenthesis were closed:
       return stack.isEmpty()
    

    The idea is that the parenthesis representing the opened scope is in the head of the stack, and each closing parenthesis – you can validate if it is closing the appropriate open parenthesis by looking the head of the stack.

    Note: It is easy to see that for a single type parenthesis we could use an integer to mimic the stack (since we only actually needed to count the number, and don’t care for the type of the parenthesis).

    Also, since a loop+stack algorithms are really similar to recursion actually, we can derive the following recursive algorithm:

    checkValidty(str,currentParenthesis,currentIndex): 
    //currentIndex is a common variable, changed by reference to affect all levels of recursion!
       while (currentIndex < str.size()):
          char <- str[currentIndex]
          currentIndex <- currentIndex + 1
          if char is open paranthesis: 
            //if the recursive call is unseccesfull - end the check, the answer is no
             if !checkValidity(str,char,currentIndex): 
                return false
          else if char is close parantesis: 
             if currentParenthesis == matchingParanthesis(char):
                return true
             else: //if the closing parenthesis do not close anything previously opened, return false
                 return false 
       //end of loop - check if all opened parenthesis were closed:
       return currentParenthesis == nil
    

    Invoke with checkValidty(str,nil,0) – where str is the validated string.

    It is easy to see that the iterative and recursive algorithms are actually the same, in the second we use the call stack and the variable lastParenthesis as the head of the stack.


    (1) The language is all the words accepted by the problem. for example (w) is in the language while )w( is not.

    (2) to be exact: some grammers need a non-deterministic automata and a stack, but this is a bit more theoretical thing, and is not the issue here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Absolute path & Relative Path This is a very basic question, but
Possible Duplicate: WITH statement in Java In Visual Basic, I could do this: With
Possible Duplicate: How to print message from caught exception? Apologies if this is basic
Possible Duplicate: Collection initialization syntax in Visual Basic 2008? This does not compile. Dim
Possible Duplicate: Can jQuery provide the tag name? Hi! This question is so basic
Possible Duplicate: Collection initialization syntax in Visual Basic 2008? How is the following C#
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: check whether internet connection is available with C# I just want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.