Given A string containing just the characters,,, ‘(‘ ‘)‘ , and ‘{‘ ‘}‘ ‘[‘ ‘]‘ , determine if the input string I S valid.
The brackets must close in the correct order, and is all valid but and is not "()" "()[]{}" "(]" "([)]" .
Solution:
1 Public classSolution {2 Public BooleanIsValid (String s) {3stack<character> stack =NewStack<character>();4 5 for(intI=0;i<s.length (); i++){6 CharCur =S.charat (i);7 if(cur== ' (' | | | cur== ' [' | | cur== ' {'))8 Stack.push (cur);9 Else {Ten if(Stack.isempty ())return false; One CharPre =Stack.pop (); A if(!ismatch (Pre,cur))return false; - } - } the - if(!stack.isempty ())return false; - Else return true; - + } - + Public BooleanIsMatch (CharACharb) { A if((a== ' (' && b== ') ') | | (a== ' [' && b== '] ') | | (a== ' {' &&b== '} ')) at return true; - Else return false; - } -}
Leetcode-valid parentheses