Similar Problems

Similar Problems not available

Encrypt And Decrypt Strings - Leetcode Solution

Companies:

LeetCode:  Encrypt And Decrypt Strings Leetcode Solution

Difficulty: Hard

Topics: string design array hash-table  

The Encrypt and Decrypt Strings problem on LeetCode requires us to implement two functions, encrypt and decrypt, that will take a message string as input and return an encrypted/decrypted string.

The encryption algorithm is as follows:

  • Split the message string into two parts: one with the even-indexed characters, and one with the odd-indexed characters.
  • Concatenate the two parts in reverse order.
  • Replace each character with its ASCII code (in decimal) representation.

The decryption algorithm is the reverse of the encryption algorithm:

  • Convert each ASCII code back to its character representation.
  • Split the resulting string in half.
  • Reverse the order of each half.
  • Interleave the two halves to get the original message.

Here is a detailed solution to this problem:

class Solution:
    def encrypt(self, message: str) -> str:
        # Step 1: Split message into even- and odd-indexed characters
        even = ""
        odd = ""
        for i in range(len(message)):
            if i % 2 == 0:
                even += message[i]
            else:
                odd += message[i]
        # Step 2: Concatenate the two parts in reverse order
        encrypted = odd + even
        # Step 3: Replace each character with its ASCII code
        ascii_encrypted = ""
        for char in encrypted:
            ascii_encrypted += str(ord(char))
            ascii_encrypted += " " # Add a space between ASCII codes
        return ascii_encrypted.strip() # Remove trailing space

    def decrypt(self, message: str) -> str:
        # Step 1: Convert ASCII codes back to characters
        ascii_decrypted = message.split() # Split by spaces
        decrypted = ""
        for code in ascii_decrypted:
            decrypted += chr(int(code))
        # Step 2: Split the resulting string in half
        mid = len(decrypted) // 2
        left = decrypted[:mid]
        right = decrypted[mid:]
        # Step 3: Reverse the order of each half
        left = left[::-1]
        right = right[::-1]
        # Step 4: Interleave the two halves to get the original message
        original = ""
        for i in range(mid):
            original += left[i]
            original += right[i]
        # If message has odd length, add the last character from the right half
        if len(decrypted) % 2 != 0:
            original += right[-1]
        return original

In the encrypt function:

  • We create two empty strings, even and odd, and iterate over the characters in the input message string.
  • If the index is even, we add the character to even, and if it is odd, we add it to odd.
  • We concatenate the two parts in reverse order to get the encrypted string.
  • Finally, we replace each character with its ASCII code representation using the ord function, and add a space between each code.

In the decrypt function:

  • We split the input message string by spaces to get a list of ASCII codes.
  • We convert each code back to its character representation using the chr function, and concatenate the characters to get the decrypted string.
  • We split the decrypted string in half at the midpoint, using integer division to get two equal-length halves (if the decrypted string has odd length, the last character will be in the right half).
  • We reverse the order of each half using the [::-1] notation.
  • We interleave the two halves by iterating over the indices from 0 to mid-1, adding the character from the left and right halves at each index to the original string.
  • Finally, if the decrypted string has odd length, we add the last character from the right half to the original string.

Encrypt And Decrypt Strings Solution Code

1