Algorithms

Caesar Cipher Encryptor

5 months ago ; 76 views
Share this

The problem being solved by 'Caesar Cipher Encryptor' solution is to perform a Caesar Cipher encryption on a given string with a specified key.

Problem Overview

The Caesar Cipher is a simple encryption technique where each letter in the plaintext is shifted a certain number of places down or up the alphabet.

For example, with a key of 3, 'A' would be encrypted to 'D', 'B' to 'E', and so on. If the end of the alphabet is reached, the shifting wraps around to the beginning.

Implementation Approach 1

# O(n) time | O(n) space
def caesar_cipher_encryptor(string, key):
	new_letters =[]
	new_key = key % 26
	for letter in string:
		new_letters.append(get_new_letter(letter,new_key))
	return "".join(new_letters)
	
def get_new_letter(letter, key):
	new_letter_code = ord(letter) + key
	return chr(new_letter_code) if new_letter_code <= 122 else chr(96 + new_letter_code % 122)

print("caesar cipher encryptor::", caesar_cipher_encryptor("xyz", 2))

Solution Overview

The solution above consists of two functions:

caesar_cipher_encryptor(string, key)

This function takes a string and a key as input.

It initializes an empty list new_letters to store the encrypted letters.

It calculates a new key by taking the modulo of the input key with 26 (the number of letters in the alphabet) to handle cases where the key is larger than 26.

Subsequently, it iterates through each letter in the input string. For each letter, it calls the get_new_letter function to calculate the new encrypted letter using the given key.

It appends the new encrypted letter to the new_letters list.

Finally, it joins the letters in the new_letters list into a single string and returns the encrypted string.

get_new_letter(letter, key)

This helper function takes a letter and a key as input.

It calculates the new ASCII code for the encrypted letter by adding the original ASCII code of the letter to the key.

If the new ASCII code exceeds the ASCII code for 'z' (122), it wraps around to the beginning of the alphabet by taking the modulo of the new code with 122 and adding 96 (the ASCII code of 'a').

It returns the character corresponding to the new ASCII code as the encrypted letter.

Time Complexity

The time complexity of the solution is O(n), where n is the length of the input string. This is because it iterates through each character in the string once to encrypt it.

Space Complexity

The space complexity of the solution is also O(n), where n is the length of the input string. This is because it creates a new list new_letters to store the encrypted letters, which can potentially be of the same length as the input string.

Implementation Approach 2

Here's an alternative to the first approach with the caveat that this implementation does not rely on the inbuilt methods used above like ord() and chr().

However, this approach has a similar time & space complexity to the above solution.

# O(n) time | O(n) space
def caesar_cipher_encryptor_updated(string, key):
	new_letters =[]
	new_key = key % 26
	alphabet="abcdefghijklmnopqrstuvwxyz"
	for letter in string:
		new_letters.append(get_new_letter_updated(letter,new_key, alphabet))
	return "".join(new_letters)
	
def get_new_letter_updated(letter, key, alphabet):
	new_letter_code = alphabet.index(letter) + key
	return alphabet[new_letter_code] if new_letter_code <= 25 else alphabet[-1 + new_letter_code % 25]
	
print("caesar cipher encryptor updated::", caesar_cipher_encryptor_updated("xyz", 2))

Become a member
Get the latest news right in your inbox. We never spam!

Read next

Island Perimeter

&nbsp;This solution seeks to find the perimeter of an island in a grid.&nbsp; The problem considers a grid where each cell represents land (1) or … Read More

Kibsoft 3 months, 3 weeks ago . 76 views

Pacific Atlantic Waterflow

3 months, 3 weeks ago . 82 views