Original problem – https://leetcode.com/problems/to-lower-case/
Github solution && Github unit tests
Problem description
Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
Example 1: Input: s = "Hello" Output: "hello" Example 2: Input: s = "here" Output: "here" Example 3: Input: s = "LOVELY" Output: "lovely"
Solution
The brute force solution here is to have a hashmap here where the key will be a letter in the lowercase and the value will be a letter in the uppecase. It gives us an option to iterate over the argument letters and search for a match in the hash map if the match is found we can append its value to the result string (probably you might want to use string builder here) and return the result.
A more smart solution on the other hand is to use bitwise operation. If we take a close look at the ASCII code of the letters you might notice that each lowercase letter has 5th bit set to ‘1’ and each uppercase letter has 5th bit set to ‘0’.
a = 01100001 A = 01000001 b = 01100010 B = 01000010 c = 01100011 C = 01000011 d = 01100100 D = 01000100 e = 01100101 E = 01000101 f = 01100110 F = 01000110 g = 01100111 G = 01000111 h = 01101000 H = 01001000 i = 01101001 I = 01001001 j = 01101010 J = 01001010 k = 01101011 K = 01001011 l = 01101100 L = 01001100 m = 01101101 M = 01001101 n = 01101110 N = 01001110 o = 01101111 O = 01001111 p = 01110000 P = 01010000 q = 01110001 Q = 01010001 r = 01110010 R = 01010010 s = 01110011 S = 01010011 t = 01110100 T = 01010100 u = 01110101 U = 01010101 v = 01110110 V = 01010110 w = 01110111 W = 01010111 x = 01111000 X = 01011000 y = 01111001 Y = 01011001 z = 01111010 Z = 01011010
The trick here is to apply the ‘OR’ operation to the 5th bit and will give us a lowercase variant of the letter. With this idea in mind, we can iterate over all the characters in the argument and switch 5th-bit using several approaches (fundamentally they are the same).
for (var i = 0; i < a.length; i++) { a[i] |= ' '; }
Here we so say adding 32 bit to the uppercase value and it will give to us an expected lowercase result. As a 32 bit here we can use an Integer = 32; a bitwise operation 1 << 5; or even whitespace (as in the example) since whitespace has 32 bits itself (but it’s better no to do so because it’s not obvious). Also, we can simply add the difference between two random but same letters to each character (in that case we do not need to memorize the exact amount of bits) arr[i] += 'a' - 'A';