LeetCode Entry

2075. Decode the Slanted Ciphertext

04.04.2026 medium 2026 kotlin rust

Decode row-encoded string

2075. Decode the Slanted Ciphertext medium substack youtube

04.04.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1318

Problem TLDR

Decode row-encoded string #medium

Intuition

Spaces at the end is not allowed for original string. That means we can simply decode with the end condition, then trim.

Approach

  • Kotlin: trimEnd

Complexity

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

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

Code

// 38ms
    fun decodeCiphertext(e: String, r: Int) = buildString {
        val skip = e.length/r + 1
        for (j in 0..<skip) for (i in 0..<r)
            if (j + i * skip < e.length) append(e[j + i * skip])
    }.trimEnd()
// 8ms
    pub fn decode_ciphertext(e: String, r: i32) -> String {
        let (s, mut ans) = (e.len() / r as usize + 1, String::new());
        for j in 0..s { for i in 0..r as usize {
            if j + i * s < e.len() {  ans.push(e.as_bytes()[j + i * s] as char)  }}}
        ans.trim_end().into()
    }