Sort by

recency

|

1264 Discussions

|

  • + 0 comments

    Java solution:

     public static boolean isLower(char c){
            return c >= 97 && c <= 122;
            }
        public static boolean isUpper(char c){
            return c >= 65 && c <= 90;
            }
       
       
        public static String caesarCipher(String s, int k) {
            String encrypted_text = "";
            int n = s.length();
            
            for(int i = 0; i < n; i++){
                char c = s.charAt(i);
                if(isUpper(c))
                    c = (char)( ( ( ( (int)c - 65) + k) % 26) + 65) ;
                else if(isLower(c))
                    c = (char)( ( ( ( (int)c - 97) + k) % 26) + 97) ;
                
                
                encrypted_text += c;
                }
            return encrypted_text;
    
            }
    
  • + 0 comments

    Beats 100% Java O(N), T(1)

     public static String caesarCipher(String s, int k) {
            char[] arr = "abcdefghijklmnopqrstuvwxyz".toCharArray(); // T(26) - > T(1)
            k %= arr.length;
              
            reverse(arr, 0, k - 1);
            reverse(arr, k, arr.length - 1 );
            reverse(arr, 0, arr.length - 1);
            //O(N)
            StringBuilder sb = new StringBuilder();
            //[c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a, b]
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (Character.isLetter(ch)) {
                    if (Character.isLowerCase(ch)) {
                        int index = ch - 'a';
                        sb.append(arr[index]);
                    } else {
                        int index = Character.toLowerCase(ch) - 'a';
                        sb.append(Character.toUpperCase(arr[index]));
                    }
                } else {
                    sb.append(ch);
                }
            }
    
            return sb.toString();
        }
        
        static void reverse(char[] arr, int start, int end) {
            while (start < end) {
                swap(arr, start, end);
                start++;
                end--;
            }
        }
        
        static void swap(char[] arr, int start, int end) {
            char temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/KApgpiqqX20

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
       string s;
       int cipher;
       cin >> cipher;
       cin >> s;
       cin >> cipher;
       for(char &c:s) {
           if(isalpha(c)){
               char a = isupper(c)? 'A':'a';
               c = a + (c - a + cipher) % 26;
           }
       }
        cout << s ;
        return 0;
    }
    
  • + 0 comments

    c++ solution this solution uses vector to store the new position of charecters after shiftting it by k element then the corrosponding indexs are retrive to build the cipher text

    {
        string cipher_text="";
        vector<char>alphabet(26);
        for (int i = 0; i < 26; ++i) {
            alphabet[i] = 'a' + (i + k) % 26;
        }
        
        for (int i=0; i<s.size();i++)
        {   
            char c=s[i];
            bool f=false;
    // edge case of uppercase we have to convert it to lowercase first then after finding the corresponding //chsr make it uppercase again
            if (c >='A'&& c<='Z')
            {
                c = tolower(s[i]);
                f=true;
            }
            
            int idx=c-'a';
            if(idx>=0 && idx<=25){
                if(f)
                {
                      cipher_text+=toupper(alphabet[idx]);
                }
                else {
                      cipher_text+=alphabet[idx];
                }
              
            }
            else
            {
                cipher_text+=c;
            }
        
        }
        return cipher_text;
    
    }
    
  • + 0 comments

    python3

    def caesarCipher(s, k):
        k = k % 26 
        lower = {chr(i): chr((i - ord('a') + k) % 26 + ord('a')) for i in range(ord('a'), ord('z') + 1)}
        upper = {chr(i): chr((i - ord('A') + k) % 26 + ord('A')) for i in range(ord('A'), ord('Z') + 1)}
    
        return ''.join([lower[char] if char in lower else upper[char] if char in upper else char for char in s])