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
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
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 . .Reading row by row gives:
. h . . p .
. . e . . r
"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
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
โ Linear Growth
Space Complexity
O(1)
Only uses variables for calculations, no additional data structures
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code