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.
1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char> sk; 5 for(int i = 0; i < s.length(); i++) { 6 if(s[i] == '(' || s[i] == '[' || s[i] == '{') { 7 sk.push(s[i]); 8 } 9 else if(sk.empty() || abs(sk.top()-s[i]) > 2) { 10 return false; 11 } 12 else { 13 sk.pop(); 14 } 15 } 16 return sk.empty(); 17 } 18 };