{"id":8715,"date":"2021-11-15T11:27:28","date_gmt":"2021-11-15T19:27:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8715"},"modified":"2021-11-15T11:28:03","modified_gmt":"2021-11-15T19:28:03","slug":"leetcode-2075-decode-the-slanted-ciphertext","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2075-decode-the-slanted-ciphertext\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2075. Decode the Slanted Ciphertext"},"content":{"rendered":"\n<p>A string&nbsp;<code>originalText<\/code>&nbsp;is encoded using a&nbsp;<strong>slanted transposition cipher<\/strong>&nbsp;to a string&nbsp;<code>encodedText<\/code>&nbsp;with the help of a matrix having a&nbsp;<strong>fixed number of rows<\/strong>&nbsp;<code>rows<\/code>.<\/p>\n\n\n\n<p><code>originalText<\/code>&nbsp;is placed first in a top-left to bottom-right manner.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/07\/exa11.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The blue cells are filled first, followed by the red cells, then the yellow cells, and so on, until we reach the end of&nbsp;<code>originalText<\/code>. The arrow indicates the order in which the cells are filled. All empty cells are filled with&nbsp;<code>' '<\/code>. The number of columns is chosen such that the rightmost column will&nbsp;<strong>not be empty<\/strong>&nbsp;after filling in&nbsp;<code>originalText<\/code>.<\/p>\n\n\n\n<p><code>encodedText<\/code>&nbsp;is then formed by appending all characters of the matrix in a row-wise fashion.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/07\/exa12.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The characters in the blue cells are appended first to&nbsp;<code>encodedText<\/code>, then the red cells, and so on, and finally the yellow cells. The arrow indicates the order in which the cells are accessed.<\/p>\n\n\n\n<p>For example, if&nbsp;<code>originalText = \"cipher\"<\/code>&nbsp;and&nbsp;<code>rows = 3<\/code>, then we encode it in the following manner:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/25\/desc2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The blue arrows depict how&nbsp;<code>originalText<\/code>&nbsp;is placed in the matrix, and the red arrows denote the order in which&nbsp;<code>encodedText<\/code>&nbsp;is formed. In the above example,&nbsp;<code>encodedText = \"ch ie pr\"<\/code>.<\/p>\n\n\n\n<p>Given the encoded string&nbsp;<code>encodedText<\/code>&nbsp;and number of rows&nbsp;<code>rows<\/code>, return&nbsp;<em>the original string<\/em>&nbsp;<code>originalText<\/code>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;<code>originalText<\/code>&nbsp;<strong>does not<\/strong>&nbsp;have any trailing spaces&nbsp;<code>' '<\/code>. The test cases are generated such that there is only one possible&nbsp;<code>originalText<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> encodedText = \"ch   ie   pr\", rows = 3\n<strong>Output:<\/strong> \"cipher\"\n<strong>Explanation:<\/strong> This is the same example described in the problem description.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/26\/exam1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> encodedText = \"iveo    eed   l te   olc\", rows = 4\n<strong>Output:<\/strong> \"i love leetcode\"\n<strong>Explanation:<\/strong> The figure above denotes the matrix that was used to encode originalText. \nThe blue arrows show how we can find originalText from encodedText.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/26\/eg2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> encodedText = \"coding\", rows = 1\n<strong>Output:<\/strong> \"coding\"\n<strong>Explanation:<\/strong> Since there is only 1 row, both originalText and encodedText are the same.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/26\/exam3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> encodedText = \" b  ac\", rows = 2\n<strong>Output:<\/strong> \" abc\"\n<strong>Explanation:<\/strong> originalText cannot have trailing spaces, but it may be preceded by one or more spaces.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= encodedText.length &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>encodedText<\/code>&nbsp;consists of lowercase English letters and&nbsp;<code>' '<\/code>&nbsp;only.<\/li><li><code>encodedText<\/code>&nbsp;is a valid encoding of some&nbsp;<code>originalText<\/code>&nbsp;that&nbsp;<strong>does not<\/strong>&nbsp;have trailing spaces.<\/li><li><code>1 &lt;= rows &lt;= 1000<\/code><\/li><li>The testcases are generated such that there is&nbsp;<strong>only one<\/strong>&nbsp;possible&nbsp;<code>originalText<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string decodeCiphertext(string encodedText, int rows) {\n    const int cols = encodedText.size() \/ rows;    \n    string ans;\n    ans.reserve(encodedText.size());\n    for (int i = 0; i < cols; ++i)\n      for (int j = 0; j < rows &#038;&#038; i + j < cols; ++j)      \n        ans += encodedText[j * cols + j + i];\n    while (ans.size() &#038;&#038; !isalpha(ans.back())) ans.pop_back();\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string&nbsp;originalText&nbsp;is encoded using a&nbsp;slanted transposition cipher&nbsp;to a string&nbsp;encodedText&nbsp;with the help of a matrix having a&nbsp;fixed number of rows&nbsp;rows. originalText&nbsp;is placed first in a top-left&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,179,4],"class_list":["post-8715","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8715"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8715\/revisions"}],"predecessor-version":[{"id":8717,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8715\/revisions\/8717"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}