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 "()" "()[]{}" "(]" "([)]" .
Subscribe to see which companies asked this question
1 Public classSolution {2 Public BooleanIsValid (String s) {3 if(s = =NULL|| S.length () = = 0)return false;4stack<character> stack =NewStack<character>();5 for(inti = 0; i< s.length (); i++){6 if(!Stack.empty ()) {7 if(S.charat (i) = = ') ' && stack.peek () = = ' (')8 Stack.pop ();9 Else if(S.charat (i) = = '] ' && stack.peek () = = ' [')Ten Stack.pop (); One Else if(S.charat (i) = = '} ' && stack.peek () = = ' {') A Stack.pop (); - Else - Stack.push (S.charat (i)); the}Else - Stack.push (S.charat (i)); - } - returnstack.empty (); + } -}
Valid Parentheses Java Solutions