|
| 1 | +2075\. Decode the Slanted Ciphertext |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +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`. |
| 6 | + |
| 7 | +`originalText` is placed first in a top-left to bottom-right manner. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +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`. |
| 12 | + |
| 13 | +`encodedText` is then formed by appending all characters of the matrix in a row-wise fashion. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +For example, if `originalText = "cipher"` and `rows = 3`, then we encode it in the following manner: |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +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"`. |
| 24 | + |
| 25 | +Given the encoded string `encodedText` and number of rows `rows`, return _the original string_ `originalText`. |
| 26 | + |
| 27 | +**Note:** `originalText` **does not** have any trailing spaces `' '`. The test cases are generated such that there is only one possible `originalText`. |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +**Input:** encodedText = "ch ie pr", rows = 3 |
| 32 | + |
| 33 | +**Output:** "cipher" |
| 34 | + |
| 35 | +**Explanation:** This is the same example described in the problem description. |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +**Input:** encodedText = "iveo eed l te olc", rows = 4 |
| 42 | + |
| 43 | +**Output:** "i love leetcode" |
| 44 | + |
| 45 | +**Explanation:** The figure above denotes the matrix that was used to encode originalText. |
| 46 | + |
| 47 | +The blue arrows show how we can find originalText from encodedText. |
| 48 | + |
| 49 | +**Example 3:** |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +**Input:** encodedText = "coding", rows = 1 |
| 54 | + |
| 55 | +**Output:** "coding" |
| 56 | + |
| 57 | +**Explanation:** Since there is only 1 row, both originalText and encodedText are the same. |
| 58 | + |
| 59 | +**Constraints:** |
| 60 | + |
| 61 | +* <code>0 <= encodedText.length <= 10<sup>6</sup></code> |
| 62 | +* `encodedText` consists of lowercase English letters and `' '` only. |
| 63 | +* `encodedText` is a valid encoding of some `originalText` that **does not** have trailing spaces. |
| 64 | +* `1 <= rows <= 1000` |
| 65 | +* The testcases are generated such that there is **only one** possible `originalText`. |
0 commit comments