Advertisement
exmkg

4-6

Jan 14th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. class Solution {
  2.     public boolean isValid(String s) {
  3.         Stack<Character> stack = new Stack<Character>();
  4.         for (char c : s.toCharArray()) {
  5.             if (c == '(') stack.push(')');
  6.             else if (c == '{') stack.push('}');
  7.             else if (c == '[') stack.push(']');
  8.             else if (stack.isEmpty() || stack.pop() != c) return false;
  9.         }
  10.         return stack.isEmpty();
  11.     }
  12. }
  13.  
  14. //     s:
  15. // stack:
  16.  
  17. //     s: )]
  18. // stack: ])
  19.  
  20. //     s: )[]
  21. // stack:
  22.  
  23. //     s:
  24. // stack: )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement