Leetcode 2075: Decode the Slanted Ciphertext

grid47
grid47
Exploring patterns and algorithms
Apr 13, 2024 6 min read

Given an encoded string and the number of rows used to encode the original text, recover the original string. The encoded text is produced by filling a matrix in a slanted transposition cipher and reading it row by row.
Problem
Approach
Steps
Complexity
Input: The input consists of an encoded string `encodedText` and an integer `rows` representing the number of rows used to encode the original text.
Example: encodedText = 'hello wor ld ', rows = 3
Constraints:
• 0 <= encodedText.length <= 10^6
• encodedText consists of lowercase English letters and spaces
• encodedText is a valid encoding of some originalText that does not have trailing spaces
• 1 <= rows <= 1000
Output: Return the decoded original text as a string.
Example: Output: 'helloworld'
Constraints:
Goal: To decode the encoded text back to its original form using the given number of rows.
Steps:
• Calculate the number of columns in the matrix from the length of the encoded text and the number of rows.
• Reconstruct the matrix by placing the characters of the encoded text according to the slanted transposition cipher pattern.
• Read the matrix diagonally and reconstruct the original text.
Goal: The constraints specify the valid range for the input size and number of rows.
Steps:
• 0 <= encodedText.length <= 10^6
• 1 <= rows <= 1000
• encodedText consists of lowercase English letters and spaces
• encodedText is a valid encoding with no trailing spaces
Assumptions:
• The encoded text does not contain trailing spaces.
Input: Example 1
Explanation: In this example, the encoded string is 'hello wor ld ' with 3 rows. By filling the matrix diagonally, we can recover the original string 'helloworld'.

Input: Example 2
Explanation: For the input 'abc defghi' and 3 rows, the matrix looks like: 'abc', 'def', 'ghi'. Reading diagonally, we recover 'abcdefghi'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus