LeetCode Entry
20. Valid Parentheses
Are braces valid
20. Valid Parentheses easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1499
Problem TLDR
Are braces valid
Intuition
Push open kind to the stack. Pop close kind if match with top of the stack.
Approach
- or push matching close brace to the stack
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun isValid(s: String) = ArrayDeque<Char>().run {
s.all{if(it in "([{") add(it+1+it.code%2)
else removeLastOrNull()==it } && isEmpty()}
pub fn is_valid(s: String) -> bool {
let mut q = vec![];
s.bytes().all(|b|if b"([{".contains(&b){q.push(b+1+b%2);true}else{q.pop()==Some(b)})&&q.is_empty()
}
Comments