Guest User

Untitled

a guest
May 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. package coding;
  2.  
  3. /**
  4. * Determine whether a given string of parentheses (single type) is properly nested.
  5. * @author Seok Kyun. Choi. 최석균 (Syaku)
  6. * @since 2018. 5. 22.
  7. */
  8. public class Nesting {
  9. public static void main(String[] args) {
  10. solution("(()(())())");
  11. solution("((((((((((((((()))))))()()()))))))))");
  12. solution("()");
  13. solution("");
  14. solution("1234");
  15. solution("(VM)");
  16. System.out.println("----------------------------");
  17. solution("())(()");
  18. solution("(()(()))())");
  19. solution("(()(())()");
  20. solution("(()))(()))");
  21. solution("(");
  22. solution("(A");
  23. solution("((");
  24. solution(")(");
  25. solution(")(()");
  26. solution("())");
  27. solution("((((((((((((((()))))))()()())))))))");
  28. }
  29.  
  30. /**
  31. * 조건
  32. * N = 0.. 1000000 정수
  33. * S = ( , ) 괄호
  34. * 제대로 중첩된다면 1, 아니면 0 을 반환
  35. */
  36. public static int solution(String S) {
  37. if (S == null || S.equals("")) return 1;
  38.  
  39. int length = S.length();
  40.  
  41. int a = 0;
  42.  
  43. for (int i = 0; i < length; i++) {
  44. char c = S.charAt(i);
  45.  
  46. if (c == ')' && i == 0) return 0;
  47.  
  48. if (c == '(') {
  49. a++;
  50. } else if (c == ')') {
  51. if (a < 1) {
  52. System.out.println("false");
  53. return 0;
  54. }
  55. a--;
  56. }
  57. }
  58.  
  59. System.out.println(a);
  60.  
  61. if (a == 0) return 1;
  62. return 0;
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment