Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Decode the Slanted Ciphertext

XPChallenge Points: 20
levelLevel: Medium

You’re given encodedText produced by writing originalText into a matrix with a fixed number of rows, filling diagonally (top-left → bottom-right), padding remaining cells with spaces, choosing cols so the last column isn’t empty, then reading the matrix row-wise to form encodedText.
Given encodedText and rows, return the unique originalText (with no trailing spaces).

Decoding idea:

  • Let cols = len(encodedText) // rows.

  • Recreate the rows × cols matrix by filling it row-wise from encodedText.

  • Read back along diagonals: for each column c from 0..cols-1, take cells (r, c+r) for r = 0..rows-1 while c+r < cols.

  • Join and rstrip() to remove trailing spaces.

Example 1:

Input: encodedText = "p rgor arming" rows = 4

Output: "programming"

Explanation:

Matrix (4 rows): p r g r a r g m i n g Reading diagonally: "programming".

Example 2:

Input: encodedText = "ivseeh ec c l ut owb"

Output: "i love wscube tech"

Explanation:

Matrix (4 rows): ivseeh ec c l ut owb Reading diagonally: "i love wscube tech".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result