Skip to content

String encoding & decoding

Description

  • Given an encoded string and the number of rows of the matrix used for encoding, return the decoded string.

  • Example:

  • input: "mnes__ya___mi"
  • output: "my name is"
  • Encoding matrix
[
  ["m", "n", "e", "s", " "],
  [" ", "y", "a", " ", " "],
  [" ", " ", " ", "m", "i"]
]

Solution steps

  • Decoding

  • First of all, check if the input parameters were passed (encodedStr and numOfRows). Also check if the number of rows is greater than zero.

  • Determine the number of columns for the matrix.
  • Populate the matrix sequentially with the letters of encodedStr.
  • Once the matrix is populate, read the letters from the matrix in the correct order and join the letters.
  • Trim the decoded string (remove trailing spaces, for example) and return the decoded result.

  • Encoding

  • Check if the input parameters were passed (str and numOfRows). Also checks if the number of rows is greater than zero.

  • Determine the number of rows for the matrix.
  • Initialize the matrix with empty values with the dimension numOfRows*numOfColumns.
  • Populate the matrix with the letters of the plain string in the correct order.
  • Join the elements of the matrix in sequential order.
  • Return the encoded string.

Runtime complexity

  • The runtime complexity for the decodeString method is based on the dimension of the encoding matrix.
  • The dimension of the matrix can be calculated with the function d = r*(n/r), where:
  • n is the length of the encodedStr input.
  • r is the integer numOfRows input.
  • Simplifying the function d = r*(n/r) we get d = n.
  • Therefore the runtime complexity is: O(n).

Tests

# install jest dependency
npm i

# run tests
npm run test