1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool isValid(string s) { unordered_map<char, char> map{ {'(', ')'}, { '{', '}' }, { '[', ']' } }; stack<char> st; if (s.size() % 2) return false; for (auto& ch : s) { if (ch == (st.size() ? map[st.top()] : '#')) st.pop(); else st.push(ch); } if (st.size()) return false; return true;
} };
|