Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Using a stack is a very natural solution in maintaining valid amounts of inverses.
boolisOpening(char i) {return (i =='['|| i =='('|| i =='{');}// need explicitly b/c i need to ignore other nonsense charsboolisClosing(char i) {return (i ==']'|| i ==')'|| i =='}');}boolisValid(string s) { stack<char> st; unordered_map<char,char> brackets = { { ']','[' },{ ')','(' },{ '}','{' } };for (auto i : s) {if (isOpening(i)) {st.push(i); }else {if (!st.empty() &&isClosing(i) &&brackets.find(i)->second ==st.top()) {st.pop(); }elseif (!isClosing(i) &&!isOpening(i)) continue;elsereturnfalse; } }returnst.empty();}intmain(int argc,char**argv) { string test ="{}{}{}"; cout << boolalpha <<isValid(test) << endl; test ="]"; cout << boolalpha <<isValid(test) << endl; test ="{"; cout << boolalpha <<isValid(test) << endl; test ="{]fdfdfdd"; cout << boolalpha <<isValid(test) << endl; test ="{[}]"; cout << boolalpha <<isValid(test) << endl; test ="dsd{()()[]}"; cout << boolalpha <<isValid(test) << endl;}
Constant Space Solution
Limitations: Only works with brackets of the same type e.g. ()() or [].
The idea is the same: I am counting the number of opening and closing parathesis types. In the end, the number of opening parathesis should match the number of closing paranthesis.
The check for (counter < 0) ensures that if a closing paranthesis ever comes before a opening, then the counter would be negative, and we cannot have that. Likewise, in the stack method.
boolisOpening(char i) {return (i =='['|| i =='('|| i =='{');} // checks openingboolisClosing(char i) {return (i ==']'|| i ==')'|| i =='}');} // checks closingconst unordered_set<char> allowed = {'[',']','(',')','{','}'}; // set of allowed parathesis types /* Uses a counter to increment or decrement the number of opening or closing paranthesis respectively. Effectively works in a similiar way using the stack method. */boolisValid(conststring&s) {int counter =0;for (char c : s) {if (allowed.find(c) ==allowed.end()) continue;if (isOpening(c)) counter++;elseif (isClosing(c)) counter--;if (counter <0) returnfalse; }return counter ==0;}intmain(){ string test; test ="))(("; cout << boolalpha <<isValid(test) << endl; // true test ="{}fdfdfdd"; cout << boolalpha <<isValid(test) << endl; // true test ="{}{sdsd}{}"; cout << boolalpha <<isValid(test) << endl; // true test ="]"; cout << boolalpha <<isValid(test) << endl; // false test ="{"; cout << boolalpha <<isValid(test) << endl; // false test ="(())"; cout << boolalpha <<isValid(test) << endl; // true test ="()()()(())()()(()))"; cout << boolalpha <<isValid(test) << endl; // false test ="(()(()))"; cout << boolalpha <<isValid(test) << endl; // true}