Skip to content

Commit

Permalink
[LeetCode] Add January 20.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 20, 2021
1 parent c6c59f8 commit 35cdc15
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions solved/LeetCode/Challenges/2020/January/20.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
var state = 0
val matcher = mapOf(
'(' to ')',
'{' to '}',
'[' to ']'
)

fun isValid(s: String): Boolean {
return parse(s) && state == s.length
}

fun parse(s: String): Boolean {
if (state >= s.length) return true
val cur = s[state]
if (cur !in matcher.keys) return true
state++
val next = parse(s)
if (!next || state >= s.length) return false
if (matcher[cur] != s[state]) return false;
state++
return parse(s)
}
}

0 comments on commit 35cdc15

Please sign in to comment.