From 35cdc15fc942661cbcf9ebf5fe8152adc3cd2cad Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 20 Jan 2021 14:05:17 +0100 Subject: [PATCH] [LeetCode] Add January 20. --- solved/LeetCode/Challenges/2020/January/20.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/20.kt diff --git a/solved/LeetCode/Challenges/2020/January/20.kt b/solved/LeetCode/Challenges/2020/January/20.kt new file mode 100644 index 00000000..84ac40e2 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/20.kt @@ -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) + } +}