Caesar Cipher

Sort by

recency

|

128 Discussions

|

  • + 0 comments

    public static String caesarCipher(String s, int k) { // Write your code here char[] strChar = s.toCharArray(); StringBuilder sb = new StringBuilder(); k = k%26;

        for(Character c : strChar){
            if(c >= 'a' && c <= 'z'){
                sb.append((char)((c - 'a' + k ) % 26 + 'a'));
            }else if(c >= 'A' && c <= 'Z'){
                sb.append((char) ((c - 'A' + k ) % 26 + 'A'));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
    
  • + 0 comments

    Python:

    def caesarCipher(s, k):
        alphabets = 'abcdefghijklmnopqrstuvwxyz'
        encrypted_str = ""
        for i in range(len(s)):
            char = s[i]
            if char.isalpha():
                next_idx = alphabets.index(char.lower()) + k
                next_letter = alphabets[next_idx % 26]
                encrypted_str += next_letter.upper() if char.isupper() else next_letter
            else:
                encrypted_str += char
        return encrypted_str
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def caesar_cipher(s, k):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(n)
        new_string = ""
        for letter in s:
            if (letter.lower() <= "z") and (letter.lower() >= "a"):
                if letter == letter.lower():
                    sum_a = ord("a")
                else:
                    sum_a = ord("A")
                new_string += chr(
                    ((ord(letter) - sum_a + k) % (ord("z") - ord("a") + 1)) + sum_a
                )
            else:
                new_string += letter
    
        return new_string
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn caesar_cipher(s: &str, k: i32) -> String {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(n)
        let mut new_string = String::with_capacity(s.len());
        let k = k as u8;
        for letter in s.chars() {
            if letter.is_alphabetic() {
                let sum_a = if letter.is_uppercase() { b'A' } else { b'a' };
                new_string.push(((letter as u8 - sum_a + k) % (b'z' - b'a' + 1) + sum_a) as char);
            } else {
                new_string.push(letter);
            }
        }
        new_string
    }
    
  • + 1 comment

    A hint before I show my python solution: Look into how the .maketrans() and .translate methods in import string work. They make creating a cipher like this much easier

    Also you use import string you can then use "string.ascii_lowercase" to generate a string of the lowercase letters a-z and "string.ascii_uppercase" to generate a string of the uppercase letters A-Z