You are viewing a single comment's thread. Return to all comments →
Time Complexity : O(n) Space Complexity: O(1)
Java 8
// a-z 97-122 // A-Z 65- 90
if(k==0){ return s;} k=k%26; int A = 65; int Z = 90; int a = 97; int z = 122; for(int i=0; i<s.length(); i++){ int currentChar = s.charAt(i); int newChar = currentChar + k; char intToCharConversion; if(currentChar >= A && currentChar <= Z){ if(newChar > Z){ int updatedChar = (A - 1)+ (newChar - Z); intToCharConversion = (char)updatedChar; s = s.substring(0, i) + intToCharConversion + s.substring(i + 1); }else{ intToCharConversion = (char)newChar; s = s.substring(0, i) + intToCharConversion + s.substring(i + 1); } }else if(currentChar >= a && currentChar <= z){ if(newChar > z){ int updatedChar = (a - 1)+ (newChar - z); intToCharConversion = (char)updatedChar; s = s.substring(0, i) + intToCharConversion + s.substring(i + 1); }else{ intToCharConversion = (char)newChar; s = s.substring(0, i) + intToCharConversion + s.substring(i + 1); } } } return s;
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
Time Complexity : O(n) Space Complexity: O(1)
Java 8
// a-z 97-122 // A-Z 65- 90