LeetCode Entry

3614. Process String with Special Operations II

17.06.2026 hard 2026 kotlin rust

K-th char ina pattern-build a string, %-reverse, , -pop

3614. Process String with Special Operations II hard substack youtube

https://dmitrysamoylenko.com/leetcode/

17.06.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1393

Problem TLDR

K-th char ina pattern-build a string, %-reverse, #-repeat, *-pop

Intuition

    // cd%#*# k=3
    // dc
    // dcdc
    // dcd
    // dcddcd  len=6
    // ...k
    // #       len=3 k=0
    // dcd
    // k
    // *       len=4
    // dcd*
    // #       len=2 k=0
    // dc
    //

Simulate the rules to find the length. Reverse operations: * adds to length, # halfs length and trims K-length, % reverses k = len-1-k

Approach

  • corner cases: k >= length, max(0, len-1)

Complexity

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

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

Code

    fun processStr(s: String, k: Long): Char {
        var l = s.fold(0L) {r,c->when(c){'*'->max(0L,r-1);'%'->r;'#'->r+r;else->r+1}}
        var k = k
        if (k < l) for (c in s.reversed()) when (c) {
            '*' -> l++; '%' -> k = l - 1 - k; '#' -> { l /= 2; k %= l }
            else -> if (k == --l) return c
        }
        return '.'
    }
    pub fn process_str(s: String, mut k: i64) -> char {
        let mut l=s.chars().fold(0,|r,c|match c{'*'=>(r-1).max(0),'%'=>r,'#'=>r+r,_=>r+1});
        if k < l { for c in s.chars().rev() { match c {
            '*' => l += 1, '%' => k = l - 1 - k, '#' => { l /= 2; k %= l }
            _ => { l -= 1; if k == l { return c } }
        } } } '.'
    }

Comments