Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- static String check(String input) {
- Stack<Character> stack=new Stack<>();
- for (int pos = 0; pos < input.length(); pos++) {
- char c = input.charAt(pos);
- if (c == '(') {
- stack.push(c);
- }
- if (c == ')') {
- if (stack.empty()) return "false";
- stack.pop();
- }
- }
- if (stack.empty()) {
- return "True";
- } else {
- return "False";
- }
- }
- public static void main(String[] args) {
- System.out.println(check("()()")); // true
- System.out.println(check("(()())")); // true
- System.out.println(check(")(")); // false
- System.out.println(check("(()")); // false
- System.out.println(check("()(")); // false
- System.out.println(check("((((")); // false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment