LeetCode Entry

3612. Process String with Special Operations I

16.06.2026 medium 2026 kotlin rust

Pattern-build a string, %-reverse, , -pop

3612. Process String with Special Operations I medium substack youtube

https://dmitrysamoylenko.com/leetcode/

16.06.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1392

Problem TLDR

Pattern-build a string, %-reverse, #-repeat, *-pop

Intuition

Just simulate the rules. In a worst case we would have 2^n time/space complexity if every letter would be c#..#

Approach

  • we can use fold
  • Rust has extend_from_within(0..)

Complexity

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

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

Code

    fun processStr(s: String) = s.fold("") { r, c ->
        when (c) {
            '*' -> r.dropLast(1); '%' -> r.reversed()
            '#' -> r + r; else -> r + c
        }
    }
    pub fn process_str(s: String) -> String {
        let mut res = vec![];
        for b in s.bytes() { match b {
            b'*' => {res.pop();}, b'%' => res.reverse(),
            b'#' => res.extend_from_within(0..), _ => res.push(b)
        }}
        String::from_utf8(res).unwrap()
    }

Comments