Guest User

Untitled

a guest
Apr 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /**
  2. * Created by sjeon on 4/19/2018.
  3. */
  4. import java.util.*;
  5. import java.math.*;
  6.  
  7. public class Main {
  8. public static String isValid(String s) {
  9. s = s.trim();
  10. int n = s.length();
  11. int cnt = 0;
  12. for (int i=0; i<n; i++) {
  13. if (s.charAt(i) == '(') {
  14. cnt += 1;
  15. } else {
  16. cnt -= 1;
  17. }
  18. if (cnt < 0) {
  19. return "NO";
  20. }
  21. }
  22. if (cnt == 0) {
  23. return "YES";
  24. } else {
  25. return "NO";Main.
  26. }
  27. }
  28. public static void main(String args[]) {
  29. Scanner sc = new Scanner(System.in);
  30. int n = sc.nextInt();
  31. sc.nextLine();
  32. while (n-- > 0) {
  33. System.out.println(isValid(sc.nextLine()));
  34. }
  35. }
  36. }
Add Comment
Please, Sign In to add comment