Ad
Stacks
Arrays
Data Types
Code
Diff
  • import java.util.*;
    
    public class MatchingBrackets {
      public static final class Brace {
          
            static final Map<Character, Character> enclosing = new HashMap<>();
            static {
              enclosing.put(')','(');
              enclosing.put('}','{');
              enclosing.put(']','[');
            }
            
            private Character braceChar;
            
            public Brace(Character braceChar) {
              this.braceChar = braceChar;
            }
          
            public boolean isEnclosingTo(Brace brace) {
              if (brace == null) {
                return false;
              }
              return brace.braceChar.equals(enclosing.get(this.braceChar));
            }
      }
      
      public static final class BraceStack {
          private Stack<Brace> stack = new Stack<>();
          
          public Brace getLastBraceOnStack() {
            if (stack.isEmpty()) {
              return null;
            }
            return stack.peek();
          }
          
          public Brace pop() {
            return stack.pop();
          }
          
          public void push(Brace brace) {
            stack.push(brace);
          }
          
          public boolean isEmpty() {
            return stack.isEmpty();
          }
      }
      public static boolean isBalanced(String braces) {
            BraceStack braceStack = new BraceStack();
            for (char nextChar : braces.toCharArray()) {
                final Brace nextBrace = new Brace(nextChar);
                
                if (nextBrace.isEnclosingTo(braceStack.getLastBraceOnStack())) {
                  braceStack.pop();
                } else {
                  braceStack.push(nextBrace);
                }
    
            }
            return braceStack.isEmpty();
        }
    }
    • import java.util.*;
    • public class MatchingBrackets {
    • public static final class Brace {
    • static final Map<Character, Character> enclosing = new HashMap<>();
    • static {
    • enclosing.put(')','(');
    • enclosing.put('}','{');
    • enclosing.put(']','[');
    • }
    • private Character braceChar;
    • public Brace(Character braceChar) {
    • this.braceChar = braceChar;
    • }
    • public boolean isEnclosingTo(Brace brace) {
    • if (brace == null) {
    • return false;
    • }
    • return brace.braceChar.equals(enclosing.get(this.braceChar));
    • }
    • }
    • public static boolean isBalanced(String braces) {
    • Stack<Brace> stack = new Stack<>();
    • public static final class BraceStack {
    • private Stack<Brace> stack = new Stack<>();
    • public Brace getLastBraceOnStack() {
    • if (stack.isEmpty()) {
    • return null;
    • }
    • return stack.peek();
    • }
    • public Brace pop() {
    • return stack.pop();
    • }
    • public void push(Brace brace) {
    • stack.push(brace);
    • }
    • public boolean isEmpty() {
    • return stack.isEmpty();
    • }
    • }
    • public static boolean isBalanced(String braces) {
    • BraceStack braceStack = new BraceStack();
    • for (char nextChar : braces.toCharArray()) {
    • final Brace nextBrace = new Brace(nextChar);
    • if (!stack.isEmpty()) {
    • Brace lastBraceOnStack = stack.peek();
    • if (nextBrace.isEnclosingTo(lastBraceOnStack)) {
    • stack.pop();
    • continue;
    • }
    • if (nextBrace.isEnclosingTo(braceStack.getLastBraceOnStack())) {
    • braceStack.pop();
    • } else {
    • braceStack.push(nextBrace);
    • }
    • stack.push(nextBrace);
    • }
    • return stack.isEmpty();
    • return braceStack.isEmpty();
    • }
    • }
Stacks
Arrays
Data Types
Code
Diff
  • import java.util.*;
    
    public class MatchingBrackets {
      public static final class Brace {
          
            static final Map<Character, Character> enclosing = new HashMap<>();
            static {
              enclosing.put(')','(');
              enclosing.put('}','{');
              enclosing.put(']','[');
            }
            
            private Character braceChar;
            
            public Brace(Character braceChar) {
              this.braceChar = braceChar;
            }
          
            public boolean isEnclosingTo(Brace brace) {
              return brace.braceChar.equals(enclosing.get(this.braceChar));
            }
      }
      
      public static boolean isBalanced(String braces) {
            Stack<Brace> stack = new Stack<>();
          
            for (char nextChar : braces.toCharArray()) {
                final Brace nextBrace = new Brace(nextChar);
                
                if (!stack.isEmpty()) {
                    Brace lastBraceOnStack = stack.peek();
                    if (nextBrace.isEnclosingTo(lastBraceOnStack)) {
                      stack.pop();
                      continue;
                    }
                }
                stack.push(nextBrace);
    
            }
            return stack.isEmpty();
        }
    }
    • import java.util.*;
    • public class MatchingBrackets {
    • public static final class Brace {
    • static final Map<Character, Character> enclosing = new HashMap<>();
    • static {
    • enclosing.put(')','(');
    • enclosing.put('}','{');
    • enclosing.put(']','[');
    • }
    • private Character braceChar;
    • public Brace(Character braceChar) {
    • this.braceChar = braceChar;
    • }
    • public boolean isEnclosingTo(Brace brace) {
    • return brace.braceChar.equals(enclosing.get(this.braceChar));
    • }
    • }
    • public static boolean isBalanced(String braces) {
    • Stack<String> stack = new Stack<>();
    • Map<String, String> enclosing = new HashMap<>();
    • enclosing.put(")","(");
    • enclosing.put("}","{");
    • enclosing.put("]","[");
    • for (char brace : braces.toCharArray()) {
    • final String strBrace = Character.toString(brace);
    • if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) {
    • stack.pop();
    • } else {
    • stack.push(strBrace);
    • Stack<Brace> stack = new Stack<>();
    • for (char nextChar : braces.toCharArray()) {
    • final Brace nextBrace = new Brace(nextChar);
    • if (!stack.isEmpty()) {
    • Brace lastBraceOnStack = stack.peek();
    • if (nextBrace.isEnclosingTo(lastBraceOnStack)) {
    • stack.pop();
    • continue;
    • }
    • }
    • stack.push(nextBrace);
    • }
    • return stack.isEmpty();
    • }
    • }