[LeetCode] 20. Valid Parentheses(Kotlin)

0

LeetCode

목록 보기
7/58
post-thumbnail

[LeetCode] 20. Valid Parentheses(Kotlin)

풀이

import java.util.Stack

class Solution {
    fun isValid(s: String): Boolean {
        val brackets = mapOf(
            ')' to '(', 
            '}' to '{', 
            ']' to '['
            )

        val st = Stack<Char>()
        for(ch in s){
            if(st.isEmpty() || ch in brackets.values){
                st.push(ch)
            }
            else if(ch in brackets.keys){
                if(st.isEmpty()) return false

                if(st.peek() == brackets[ch]!!) st.pop()
                else st.push(ch)
            }
            else return false //wrong input
        }
        return st.isEmpty()
    }   
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글