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
Decode the Slanted Ciphertext INPUT encodedText = "ch ie pr" rows = 3 Matrix (3 x 3): c h i e p r Blue cells filled diagonally form "cipher" cols = len/rows = 8/3 = 3 Reading row-wise ALGORITHM STEPS 1 Calculate Columns cols = len(encoded) / rows 2 Build 2D Matrix Reshape string to grid 3 Read Diagonally For each column, go down-right 4 Strip Trailing Spaces Return cleaned result Position Calculation: col row char 0 0,1,2 c,i,p 1 0,1,2 h,e,r 2 0,1 , pos = row*cols + col + row Result: c-i-p-h-e-r FINAL RESULT Decoded String: c i p h e r Reading Order: c h i e p r 1: c 2: i 3: p 4: h 5: e 6: r Output: "cipher" OK - Decoded Successfully! Key Insight: The slanted cipher writes text diagonally (top-left to bottom-right) across a matrix, then reads row-wise. To decode: rebuild the matrix from row-wise data, then read each diagonal starting from each column in row 0. Formula: position = row * cols + col + row, where we increment both row and col together for diagonal traversal. TutorialsPoint - Decode the Slanted Ciphertext | Direct Position Calculation
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