Decode the Slanted Ciphertext - Problem
Decode the Slanted Ciphertext is a fascinating string manipulation problem that simulates a unique transposition cipher technique.

You're given an encoded string that was created using a slanted transposition cipher. The original text was encoded by placing characters diagonally in a matrix (from top-left to bottom-right), then reading the matrix row by row to create the encoded string.

Your mission: Reverse this process to recover the original text!

Here's how the encoding works:
1. Create a matrix with a fixed number of rows
2. Fill characters diagonally (slanted pattern) from the original text
3. Empty cells are filled with spaces
4. Read the matrix row by row to create the encoded text

For example, if originalText = "cipher" and rows = 3:
c . . i . . 
. h . . p .
. . e . . r
Reading row by row gives: "c i h p e r" (spaces preserved)

Input & Output

example_1.py โ€” Basic Example
$ Input: encodedText = "ch ie pr", rows = 3
โ€บ Output: "cipher"
๐Ÿ’ก Note: The matrix is filled row by row, then diagonals are extracted. First diagonal: cโ†’hโ†’e (positions 0,5,10), second diagonal: iโ†’p (positions 3,7), third diagonal: r (position 11).
example_2.py โ€” Single Row
$ Input: encodedText = "iveo eed l te olc", rows = 4
โ€บ Output: "i love leetcode"
๐Ÿ’ก Note: With 4 rows and longer text, multiple diagonals are extracted. Each diagonal starts from a different column in row 0 and moves diagonally down-right.
example_3.py โ€” Edge Case
$ Input: encodedText = " b ac", rows = 2
โ€บ Output: " abc"
๐Ÿ’ก Note: Even with spaces at the beginning, the diagonal extraction works correctly. The space is part of the first diagonal.

Visualization

Tap to expand
Slanted Cipher DecodingStep 1: Matrix VisualizationencodedText = "ch ie pr" (read row by row)ch ieh p erStep 2: Extract DiagonalsRed diagonal: c โ†’ h โ†’ eBlue diagonal: i โ†’ pGreen diagonal: rStep 3: Result"cipher"Algorithm: Direct Position Calculationfor startCol in range(cols):row, col = 0, startColwhile row < rows and col < cols:pos = row * cols + colresult += encodedText[pos]row += 1; col += 1
Understanding the Visualization
1
Understand the Matrix
The encoded string represents a matrix read row by row
2
Calculate Dimensions
cols = length รท rows gives us the matrix dimensions
3
Extract Diagonals
Follow diagonal paths from each column in row 0
4
Build Result
Concatenate all diagonal sequences to get original text
Key Takeaway
๐ŸŽฏ Key Insight: The slanted cipher places characters diagonally but reads row-wise. To decode, we reverse this by extracting diagonal sequences using calculated positions, achieving optimal O(n) time and O(1) space complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the encoded string, visiting each character exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only uses variables for calculations, no additional data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค rows โ‰ค 1000
  • 1 โ‰ค encodedText.length โ‰ค 104
  • encodedText consists of lowercase English letters and spaces
  • encodedText.length is divisible by rows
  • The original text does not have trailing spaces
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
24.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen