Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by sjeon on 4/19/2018.
- */
- import java.util.*;
- import java.math.*;
- public class Main {
- public static String isValid(String s) {
- s = s.trim();
- int n = s.length();
- int cnt = 0;
- for (int i=0; i<n; i++) {
- if (s.charAt(i) == '(') {
- cnt += 1;
- } else {
- cnt -= 1;
- }
- if (cnt < 0) {
- return "NO";
- }
- }
- if (cnt == 0) {
- return "YES";
- } else {
- return "NO";Main.
- }
- }
- public static void main(String args[]) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- sc.nextLine();
- while (n-- > 0) {
- System.out.println(isValid(sc.nextLine()));
- }
- }
- }
Add Comment
Please, Sign In to add comment