In: Computer Science
Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and curly braces. Please design an efficient algorithm to determine whether the parentheses in the expression are correctly paired. Please write down the algorithm idea and steps.
Please explain what kind of data structure is used to solve the problem, and then give the specific algorithm steps
Function checkParentheses(expression):
1. Declare a stack of characters
2. Now start traversing the expression character by character
3 if current character is a opening one i.e, ( or { or [ then,
4. push it into stack
5. else if current character is a closing one i.e, ) or } or ] then,
6. pop the top character from stack
7. and check if the popped opening bracket matches current closing one
8. if matches then continue
9. else return false i.e, unbalanced parentheses
10. After the matching if we did not get to the result then
11. check if stack is empty then return true
12. otherwise return false
So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.