Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean isValid(String s) {
- // Create an empty stack to keep track of opening brackets
- Stack<Character> stack = new Stack<Character>();
- // Loop through every character in the string
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- // If the character is an opening bracket, push it onto the stack
- if (c == '(' || c == '[' || c == '{') {
- stack.push(c);
- } else { // If the character is a closing bracket
- // If the stack is empty, there is no matching opening bracket, so return false
- if (stack.isEmpty()) {
- return false;
- }
- // Otherwise, get the top of the stack and check if it's the matching opening bracket
- char top = stack.peek();
- if ((c == ')' && top == '(') || (c == ']' && top == '[') || (c == '}' && top == '{')) {
- // If it is, pop the opening bracket from the stack
- stack.pop();
- } else { // Otherwise, the brackets don't match, so return false
- return false;
- }
- }
- }
- // If the stack is empty, all opening brackets have been closed, so return true
- // Otherwise, there are unmatched opening brackets, so return false
- return stack.isEmpty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement