Sort by

recency

|

1286 Discussions

|

  • + 0 comments

    My python solution

    s2 = ''
        for ch in s :
            if ch.isalpha():
                t = ord(ch)
                if 96 <= t <= 122 :
                    t += k
                    while t > 122 :
                        t -= 26
                elif 65 <= t <= 90 :
                    t += k 
                    while t > 90 :
                        t -= 26
                s2 += chr(t)
            else :
                s2 += ch
        return s2
    
  • + 0 comments
    static Map<Character, Integer> smallcharintmap = new HashMap<>();
    static Map<Integer, Character> smallintcharmap = new HashMap<>();
    
    static Map<Character, Integer> capscharintmap = new HashMap<>();
    static Map<Integer, Character> capsintcharmap = new HashMap<>();
    
    static {
        char smallstart = 'a';
        char capstart = 'A';
    
        for(int i=1;i<=26;i++) {
            smallcharintmap.put(smallstart, i);
            smallintcharmap.put(i, smallstart);
            smallstart++;
    
            capscharintmap.put(capstart, i);
            capsintcharmap.put(i, capstart);
            capstart++;
        }
    }
    
    public static String caesarCipher(String str, int k) {
        StringBuilder resultBuilder = new StringBuilder();
        for(int i = 0; i < str.length(); i++) {
            Character ch = str.charAt(i);
            if(Character.isAlphabetic(ch)) {
                if(Character.isLowerCase(ch)) {
                    int key = (smallcharintmap.get(ch) + k)%26;
                    resultBuilder.append(smallintcharmap.get(key == 0 ? 26 : key));
                } else if(Character.isUpperCase(ch)) {
                    int key = (capscharintmap.get(ch) + k)%26;
                    resultBuilder.append(capsintcharmap.get(key == 0? 26 : key));
                }
            } else {
                resultBuilder.append(ch);
            }
        }
        return resultBuilder.toString();
    }
    
  • + 0 comments
    int base;
    for(int i=0; i<s.size(); i++){
            if(isalpha(s[i])){
                    if(islower(s[i])){
                            base = 'a';
                    }else{
                            base = 'A';
                    }
                    s[i] = ((s[i] - base + k) % 26) + base;
            }
    }
    return s;
    

    }

  • + 0 comments
    def getEncryptedLetter(s, k, isUpper=True):
        startChar = "A" if isUpper else "a"
        endChar = "Z" if isUpper else "z"
        num = ord(s)
        k = k % 26
        if num + k > ord(endChar):
            diff = (num + k) - ord(endChar)
            new_num = ord(startChar) + diff - 1
        else:
            new_num = num + k
        return chr(new_num)
        
        
    def caesarCipher(s, k):
        # Write your code here
        excrypted_str = ""
        for i in s:
            if ("A" <= i <= "Z") or ("a" <= i <= "z"):
                excrypted_str += getEncryptedLetter(i, k, "A" <= i <= "Z")
                continue
            excrypted_str += i
        return excrypted_str
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-caesar-cipher-problem-solution.html