Computer Science, asked by harikaraoenuganti, 5 months ago

sequence of parentheses is called balanced if it consists entirely of pairs of opening/closing parentheses (in that order), which is well nested. For example, sequences "(())()", "()" and "(()(()))" are balanced, while "(()" and "(()))(" are not. Write a function to determine if a given string contains balanced sequence of parentheses . write a algorithm to determine if a given string contains a balanced sequence of parentheses

Answers

Answered by advit9500
0

Answer:

So Here Is Your Answer With Full Explaination.

Explanation:

Its A Request Please mark As Brainlist

Attachments:
Answered by dreamrob
0

Check for balanced parentheses

Expression                   Balanced      

(A + B)                               yes        

{(A + B) + (C + D)}              yes      

{(A + B) + (Z)                       no        

(A + B) + (A)]                       no        

{A +B)                                  no    

For balanced parentheses, Count of opening = Counting of closing    

Last opened first closed.

Solution :

  1. Scan from left to right
  2. If opening symbol, Push it into a Stack
  3. If closing symbol is at the top of the stack opening of same type Pop
  4. Should end with an empty list

CheckBalanceParenthesis(exp)

{

  n ← length(exp)

  Create a stack :- S

  for i ← 0 to n-1

  {

     if exp[i] is '('' or '{' or '['

        Push(exp[i])

     else if exp[i] is ')' or '}' or ']'

     {  

        if (S is empty) ║ (top does not pair with exp[i])

        {

           return false

        }

        else

           POP()

     }

  }

  return S is empty ? true : false

}

Similar questions