Consider a set of (nested) list structures described by a context-free grammar w
ID: 3723256 • Letter: C
Question
Consider a set of (nested) list structures described by a context-free grammar with the fol- lowing productions: S (A) B a 1 (A) Find a pushdown automaton which accepts only those (correct) list structures which con- tain not more then three elements at each level; for example, "(a, (a,a,a), ((a),a))" and "((a),a, ((a,a),a))" should be accepted but "((a),a, (a,a),a)" should not. Hint: Use consecutive elements of the stack as counters of the number of elements on the corresponding levels of the analyzed list structure.Explanation / Answer
You can manupulate the underline code of valid parenthesis automation check
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
for (char& c : s) {
switch (c) {
case '(':
case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
default: ; // pass
}
}
return paren.empty() ;
}
};