返回介绍

solution / 2000-2099 / 2075.Decode the Slanted Ciphertext / README_EN

发布于 2024-06-17 01:03:11 字数 6264 浏览 0 评论 0 收藏 0

2075. Decode the Slanted Ciphertext

中文文档

Description

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.

 

Example 1:

Input: encodedText = "ch   ie   pr", rows = 3
Output: "cipher"
Explanation: This is the same example described in the problem description.

Example 2:

Input: encodedText = "iveo  eed   l te   olc", rows = 4
Output: "i love leetcode"
Explanation: The figure above denotes the matrix that was used to encode originalText. 
The blue arrows show how we can find originalText from encodedText.

Example 3:

Input: encodedText = "coding", rows = 1
Output: "coding"
Explanation: Since there is only 1 row, both originalText and encodedText are the same.

 

Constraints:

  • 0 <= encodedText.length <= 106
  • encodedText consists of lowercase English letters and ' ' only.
  • encodedText is a valid encoding of some originalText that does not have trailing spaces.
  • 1 <= rows <= 1000
  • The testcases are generated such that there is only one possible originalText.

Solutions

Solution 1

class Solution:
  def decodeCiphertext(self, encodedText: str, rows: int) -> str:
    ans = []
    cols = len(encodedText) // rows
    for j in range(cols):
      x, y = 0, j
      while x < rows and y < cols:
        ans.append(encodedText[x * cols + y])
        x, y = x + 1, y + 1
    return ''.join(ans).rstrip()
class Solution {
  public String decodeCiphertext(String encodedText, int rows) {
    StringBuilder ans = new StringBuilder();
    int cols = encodedText.length() / rows;
    for (int j = 0; j < cols; ++j) {
      for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) {
        ans.append(encodedText.charAt(x * cols + y));
      }
    }
    while (ans.length() > 0 && ans.charAt(ans.length() - 1) == ' ') {
      ans.deleteCharAt(ans.length() - 1);
    }
    return ans.toString();
  }
}
class Solution {
public:
  string decodeCiphertext(string encodedText, int rows) {
    string ans;
    int cols = encodedText.size() / rows;
    for (int j = 0; j < cols; ++j)
      for (int x = 0, y = j; x < rows && y < cols; ++x, ++y)
        ans += encodedText[x * cols + y];
    while (ans.back() == ' ') ans.pop_back();
    return ans;
  }
};
function decodeCiphertext(encodedText: string, rows: number): string {
  const cols = Math.ceil(encodedText.length / rows);
  let ans = [];
  for (let k = 0; k <= cols; k++) {
    for (let i = 0, j = k; i < rows && j < cols; i++, j++) {
      ans.push(encodedText.charAt(i * cols + j));
    }
  }
  return ans.join('').trimEnd();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文