Similar Problems
Similar Problems not available
Decode The Slanted Ciphertext  Leetcode Solution
Companies:
LeetCode: Decode The Slanted Ciphertext Leetcode Solution
Difficulty: Medium
Topics: string simulation
Problem Statement:
You are given a slanted ciphertext which includes lowercase English letters ('a' to 'z') and spaces. A slanted ciphertext is obtained by writing all the characters of a column in a diagonal descent order from left to right, and starting from the topmost cell of this column.
For example, the green characters of the slanted ciphertext below represent a column, while the blue characters represent the diagonal order of the same column:
a b c d a e f g h i j k l
Your task is to decode the ciphertext by rearranging the characters to their respective rows and columns.
For example, the above slanted ciphertext can be decoded to "adgj abc ehk f i l".
Write a function decode() to decode the slanted ciphertext.
Function Signature:
def decode(slanted_ciphertext: str) > str:
Input
The input parameter slanted_ciphertext is a string contains lowercase English letters ('a' to 'z') and spaces. You can assume that 1 <= len(slanted_ciphertext) <= 10^6.
Output
The function should return a string that is the decoded message.
Test Cases:
Input:
slanted_ciphertext = "a b c d e f g h i j k l"
Output:
"adgj abc ehk f i l"
Input:
slanted_ciphertext = "t e l oaa rehtom er o l a c "
Output:
"the secret message is aocelerato"
Solution:
The problem statement is asking to rearrange the slanted ciphertext to their respective rows and columns. We will solve the problem in two steps.
Step 1: Preprocessing
In this step, we will preprocess the slanted ciphertext to get the row and column information. We can observe that the length of each row is not equal, so we will need to first calculate the max length of all the rows. This will help us in creating a 2D array with the dimensions as the number of rows and max length as columns.
Let's take the example of the slanted ciphertext "a b c d e f g h i j k l". The slanted ciphertext can be split into rows as shown below:
a b c d a e f g h i j k l
We will create a 2D array arr with dimensions equal to the number of rows and max columns. Then we will iterate through the slanted ciphertext, column by column, and fill the arr row by row. This can be done by keeping track of the row number and column number.
Algorithm:
 Initialize an empty list with the name arr.
 Split the slanted_ciphertext by space character ' ' to get the words.
 Initialize two variables row, and column to zero.
 Iterate over the words
 If row == 0: Append an empty list to arr.
 If the length of the current word + column == len(arr[row]), then append the current word to arr[row] and increase the value of row and set column to zero.
 Else, fill the current empty space with the character '_' and increase the value of column.
 Join the characters rowwise in the array arr.
Here is the code snippet for the preprocessing step:
def preprocess(slanted_ciphertext: str) > List[List[str]]: arr = [] # to store the 2D array of words words = slanted_ciphertext.split() # split the slanted ciphertext to get the words max_col = 0 # to store the maximum length of all the rows
row = 0 # to store the current row number
col = 0 # to store the current column number
for w in words:
if row == 0:
arr.append([])
if len(arr[row]) == col:
arr[row].append(w)
row += 1
col = 0
else:
arr[row].append('_')
col += 1
max_col = max(max_col, len(arr[row1]))
# join the characters rowwise and return as a string
return [''.join(x).replace('_', '') for x in arr]
Step 2: Decoding
In this step, we will decode the slanted ciphertext by rearranging the characters to their respective rows and columns. We will use the arr 2D array obtained from the preprocessing step to implement the decoding step.
We will iterate through the 2D array arr column by column and append each character to the result string.
Algorithm:
 Call the preprocess function to get the 2D array arr.
 Initialize an empty string with the name res.
 Iterate through the column of arr.
 Iterate through the row of arr

If the arr[row][column] is not '_', append the character to res.
 Return the res string.
Here is the code snippet for the decoding step:
def decode(slanted_ciphertext: str) > str: arr = preprocess(slanted_ciphertext) res = ""
# iterate columnwise and append each character to res
for col in range(len(arr[0])):
for row in range(len(arr)):
if arr[row][col] != '_':
res += arr[row][col]
return res
Finally, we can call the decode function to get the decoded message.
Let's test the solution on the given test cases:
Test Case 1:
Input:
slanted_ciphertext = "a b c d e f g h i j k l"
Output:
"adgj abc ehk f i l"
assert decode("a b c d e f g h i j k l") == "adgj abc ehk f i l"
Test Case 2:
Input:
slanted_ciphertext = "t e l oaa rehtom er o l a c "
Output:
"the secret message is aocelerato"
assert decode("t e l oaa rehtom er o l a c ") == "the secret message is aocelerato"
Decode The Slanted Ciphertext Solution Code
1