LeetCode Entry

1021. Remove Outermost Parentheses

08.10.2026 easy 2026 kotlin rust

Remove outer braces

1021. Remove Outermost Parentheses easy substack youtube

https://dmitrysamoylenko.com/leetcode/

08.10.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1506

Problem TLDR

Remove outer braces

Intuition

Compute running depth; filter out depth zero.

Approach

  • Rust: .retain

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

    fun removeOuterParentheses(s: String)=run {
        var d = 0
        s.filter { 0 < if (it<')') d++ else --d }
    }
    pub fn remove_outer_parentheses(mut s: String) -> String {
        let mut d = 0;
        s.retain(|c| { d += 81 - 2 * c as i32; d + c as i32 % 2 > 1 });s
    }

Comments