g0001_0100.s0020_valid_parentheses.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 g0001_0100.s0020_valid_parentheses
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings
// #2022_10_06_Time_226_ms_(72.53%)_Space_34.1_MB_(76.91%)
import java.util.Stack
class Solution {
fun isValid(s: String): Boolean {
val stack = Stack()
for (element in s) {
val c = element
if (c == '(' || c == '[' || c == '{') {
stack.push(c)
} else if (c == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop()
} else if (c == '}' && !stack.isEmpty() && stack.peek() == '{') {
stack.pop()
} else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop()
} else {
return false
}
}
return stack.isEmpty()
}
}