![JAR search and dependency download from the Maven repository](/logo.png)
g1101_1200.s1106_parsing_a_boolean_expression.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1101_1200.s1106_parsing_a_boolean_expression
// #Hard #String #Stack #Recursion #2023_05_31_Time_205_ms_(100.00%)_Space_49.5_MB_(50.00%)
class Solution {
private var source: String? = null
private var index = 0
fun parseBoolExpr(expression: String?): Boolean {
source = expression
index = 0
return expr()
}
private fun expr(): Boolean {
val res: Boolean
res = if (match('!')) {
not()
} else if (match('&')) {
and()
} else if (match('|')) {
or()
} else {
bool()
}
return res
}
private fun not(): Boolean {
consume('!')
return !group()[0]
}
private fun or(): Boolean {
consume('|')
var res = false
for (e in group()) {
res = res or e
}
return res
}
private fun and(): Boolean {
consume('&')
var res = true
for (e in group()) {
res = res and e
}
return res
}
private fun group(): List {
consume('(')
val res: MutableList = ArrayList()
while (!match(')')) {
res.add(expr())
if (match(',')) {
advance()
}
}
consume(')')
return res
}
private fun bool(): Boolean {
val isTrue = match('t')
advance()
return isTrue
}
private val isAtEnd: Boolean
get() = index >= source!!.length
private fun advance() {
if (isAtEnd) {
return
}
index++
}
private fun peek(): Char {
return source!![index]
}
private fun match(ch: Char): Boolean {
return if (isAtEnd) {
false
} else peek() == ch
}
private fun consume(ch: Char) {
if (!match(ch)) {
return
}
advance()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy