Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coding;
- /**
- * Determine whether a given string of parentheses (single type) is properly nested.
- * @author Seok Kyun. Choi. 최석균 (Syaku)
- * @since 2018. 5. 22.
- */
- public class Nesting {
- public static void main(String[] args) {
- solution("(()(())())");
- solution("((((((((((((((()))))))()()()))))))))");
- solution("()");
- solution("");
- solution("1234");
- solution("(VM)");
- System.out.println("----------------------------");
- solution("())(()");
- solution("(()(()))())");
- solution("(()(())()");
- solution("(()))(()))");
- solution("(");
- solution("(A");
- solution("((");
- solution(")(");
- solution(")(()");
- solution("())");
- solution("((((((((((((((()))))))()()())))))))");
- }
- /**
- * 조건
- * N = 0.. 1000000 정수
- * S = ( , ) 괄호
- * 제대로 중첩된다면 1, 아니면 0 을 반환
- */
- public static int solution(String S) {
- if (S == null || S.equals("")) return 1;
- int length = S.length();
- int a = 0;
- for (int i = 0; i < length; i++) {
- char c = S.charAt(i);
- if (c == ')' && i == 0) return 0;
- if (c == '(') {
- a++;
- } else if (c == ')') {
- if (a < 1) {
- System.out.println("false");
- return 0;
- }
- a--;
- }
- }
- System.out.println(a);
- if (a == 0) return 1;
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment