I am going over these examples because i want to understand algorithms better but there are no solutions for this question.Can anybody show me what this algorithm would look like?
Design an algorithm that checks whether a string is parenthetically correct in time proportional to the size of the string using a stack as an auxiliary data structure.
Design an algorithm that checks whether a string is parenthetically correct in time proportional to the size of the string using a stack as an auxiliary data structure.
Here is some background information for the question.
1 2 3 4 5 6 7 8 9 10 11
In most programming languages, parenthetic symbols (), [],
and {} must be balanced and properly nested.
We define a parenthetically correct string of characters as a string
that matches one of the following patterns,
where S denotes a (possibly empty) string without any parenthetic
symbols, and P, P’, and P’’ recursively denote parenthetically correct strings:
S
P’(P)P’’
P’[P]P’’
P’{P}.
Hopefully this helps you understand the question better so that you can show me what the algorithm looks like. ^_^
The problem is explained exceptionally well, IMO. Traverse a string and push "opening" symbols to the stack. When "closing" symbols are encountered, verify that the top of the stack is the matching "opening" symbol and pop it off. If the symbols do not match, return an error. Finally, if all is well, verify that the stack is empty.
For example, given "([{]})":
push (
push [
push {
found {, not [
error
Given "([][])":
push (
push [
pop [
push [
pop [
pop (
stack empty
good to go
Ok i see what your doing moorecm but is there you can probably explain it a little further so that i may better understand it? Oh and thank you all for the help :D