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
Answer:
So Here Is Your Answer With Full Explaination.
Explanation:
Its A Request Please mark As Brainlist
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 :
- Scan from left to right
- If opening symbol, Push it into a Stack
- If closing symbol is at the top of the stack opening of same type Pop
- 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
}