Decode the Slanted Ciphertext - Problem

A string originalText is encoded using a slanted transposition cipher to a string encodedText with the help of a matrix having a fixed number of rows rows.

originalText is placed first in a top-left to bottom-right manner.

The blue cells are filled first, followed by the red cells, then the yellow cells, and so on, until we reach the end of originalText. The arrow indicates the order in which the cells are filled. All empty cells are filled with ' '. The number of columns is chosen such that the rightmost column will not be empty after filling in originalText.

encodedText is then formed by appending all characters of the matrix in a row-wise fashion.

The characters in the blue cells are appended first to encodedText, then the red cells, and so on, and finally the yellow cells. The arrow indicates the order in which the cells are accessed.

For example, if originalText = "cipher" and rows = 3, then we encode it in the following manner:

The blue arrows depict how originalText is placed in the matrix, and the red arrows denote the order in which encodedText is formed. In the above example, encodedText = "ch ie pr".

Given the encoded string encodedText and number of rows rows, return the original string originalText.

Note: originalText does not have any trailing spaces ' '. The test cases are generated such that there is only one possible originalText.

Input & Output

Example 1 — Basic Case
$ Input: encodedText = "ch ie pr", rows = 3
Output: cipher
💡 Note: The matrix is 3×3. Fill row by row: [[c,h, ],[i,e, ],[p,r, ]]. Read diagonally: c→i→p, h→e, (space) gives "cipher"
Example 2 — Single Row
$ Input: encodedText = "iveo eed l te olc", rows = 4
Output: i love leetcode
💡 Note: Matrix is 4×6. After filling and reading diagonally: i→v→e→o→(space)→(space), giving the original text with trailing spaces removed
Example 3 — One Row Only
$ Input: encodedText = "coding", rows = 1
Output: coding
💡 Note: With only 1 row, no transformation occurs. The encoded text is the same as original text

Constraints

  • 1 ≤ rows ≤ 1000
  • 1 ≤ encodedText.length ≤ 2000
  • encodedText consists of lowercase English letters and spaces
  • originalText does not have trailing spaces

Visualization

Tap to expand
Slanted Ciphertext Decoding ProcessInput: "ch ie pr"ch ie prMatrix (3×3):ch ie pr Read Diagonally:cipherResult: "cipher"
Understanding the Visualization
1
Encoded Input
Row-wise encoded text: "ch ie pr" with 3 rows
2
Matrix Reconstruction
Fill 3×3 matrix row by row, then read diagonally
3
Original Output
Diagonal reading gives: "cipher"
Key Takeaway
🎯 Key Insight: The slanted cipher fills diagonally but reads row-wise - we reverse by filling row-wise and reading diagonally
Asked in
Google 12 Microsoft 8 Amazon 6
15.2K Views
Medium Frequency
~15 min Avg. Time
342 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