[프로그래머스] 괄호 회전하기(Kotlin)
풀이
import java.util.*
class Solution {
fun Char.isOpen():Boolean{
if((this == '(')||(this == '{')||(this == '['))return true
return false
}
fun isCorrect(s:String):Boolean{
var stack = Stack<Char>()
for(ch in s){
if(ch.isOpen()) stack.push(ch)
else{
if(stack.isNotEmpty()){
val peekCh = stack.peek()
if((ch == ')') && (peekCh == '(')) stack.pop()
else if((ch == '}') && (peekCh == '{')) stack.pop()
else if((ch == ']') && (peekCh == '[')) stack.pop()
else return false;
}
else return false;
}
}
if(stack.isNotEmpty()) return false
return true;
}
fun solution(s: String): Int {
var answer: Int = 0
for(i in 0 .. s.length-1){
val newS = s.substring(i, s.length)+s.substring(0, i)
if(isCorrect(newS)) answer++
}
return answer
}
}