|
| 1 | +# [Problem 2075: Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I remember this problem: encodedText is produced by filling a rows x cols matrix diagonally (top-left to bottom-right) with originalText, then reading the matrix row-wise to produce encodedText. The encodedText length n and rows are given, so the number of columns is n // rows (encodedText is constructed row-wise, so each row contributes exactly cols characters). |
| 5 | +First thought: reconstruct the matrix row-by-row from encodedText, then read the original by traversing diagonals starting at each column of the top row (or equivalently, for each starting column, walk down-right). That should recover the original characters in order. Finally strip trailing spaces (original has no trailing spaces). |
| 6 | + |
| 7 | +## Refining the problem, round 2 thoughts |
| 8 | +- Edge cases: empty encodedText -> return "". rows == 1 -> original == encodedText directly. |
| 9 | +- We don't actually need to build a 2D array; we can index into the encodedText string directly since the matrix element at (r, c) equals encodedText[r * cols + c]. |
| 10 | +- For each starting column c from 0 to cols-1, for row r from 0 to rows-1, include character at index r*cols + (c + r) as long as c + r < cols. |
| 11 | +- Append characters to a list and join, then rstrip() to remove trailing spaces (originalText does not have trailing spaces). |
| 12 | +- Time complexity: O(n) where n = len(encodedText) (we visit each character at most once). Space complexity: O(n) for the output (plus O(1) extra). |
| 13 | + |
| 14 | +## Attempted solution(s) |
| 15 | +```python |
| 16 | +class Solution: |
| 17 | + def decodeCiphertext(self, encodedText: str, rows: int) -> str: |
| 18 | + # handle trivial cases |
| 19 | + if not encodedText: |
| 20 | + return "" |
| 21 | + if rows == 1: |
| 22 | + return encodedText |
| 23 | + |
| 24 | + n = len(encodedText) |
| 25 | + cols = n // rows # number of columns in the matrix |
| 26 | + res_chars = [] |
| 27 | + |
| 28 | + # For each starting column in the top row, walk diagonally down-right |
| 29 | + for start_col in range(cols): |
| 30 | + r = 0 |
| 31 | + while r < rows and start_col + r < cols: |
| 32 | + idx = r * cols + (start_col + r) |
| 33 | + res_chars.append(encodedText[idx]) |
| 34 | + r += 1 |
| 35 | + |
| 36 | + # originalText has no trailing spaces |
| 37 | + return ''.join(res_chars).rstrip() |
| 38 | +``` |
| 39 | +- Notes: |
| 40 | + - We avoid creating an explicit 2D matrix by computing the index into encodedText directly: matrix[r][c] is at encodedText[r * cols + c]. |
| 41 | + - Time complexity: O(n) where n = len(encodedText) because each character is visited at most once in the diagonal traversal. |
| 42 | + - Space complexity: O(n) for the output (res_chars), plus O(1) extra auxiliary space. |
| 43 | + - The final .rstrip() removes any trailing spaces introduced by padding in the encoding process; the problem guarantees originalText has no trailing spaces. |
0 commit comments