Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions problems/programmers/others/p76502/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* (76502) 괄호 회전하기
* https://school.programmers.co.kr/learn/courses/30/lessons/76502
*/

import java.util.Stack;

class Solution {
public int solution(String s) {
final int len = s.length();
int answer = 0;

for (int count = 0; count < len; count++) {
s = shiftLeft(s);
if (isCorrect(s)) answer++;
}

return answer;
}

private String shiftLeft(String s) {
return s.substring(1) + s.charAt(0);
}

private boolean isCorrect(String s) {
Stack<Character> charStack = new Stack<>();

for (char ch : s.toCharArray()) {
if (ch == '(' || ch == '[' || ch == '{') {
charStack.push(ch);
} else if (charStack.isEmpty()) {
return false;
} else {
char top = charStack.pop();

if (
(top == '(' && ch != ')') ||
(top == '[' && ch != ']') ||
(top == '{' && ch != '}')
) {
return false;
}
}
}

return charStack.isEmpty();
}

public static void main(String[] args) {
System.out.println(new Solution().solution("[](){}")); // 3
System.out.println(new Solution().solution("}]()[{")); // 2
System.out.println(new Solution().solution("[)(]")); // 0
System.out.println(new Solution().solution("}}}")); // 0
}
}