Leet Code — 709. To Lower Case

Ben Pereira - Aug 31 '23 - - Dev Community

This is an easy problem with the description being:

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"

Constraints:

1 <= s.length <= 100
s consists of printable ASCII characters.

At first glance, using the String methods you can straight away call toLowerCase method and that will do:

class Solution {
    public String toLowerCase(String s) {
        return s.toLowerCase();
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 0 ms, faster than 100.00% of Java online submissions for To Lower Case.

Memory Usage: 40.3 MB, less than 86.96% of Java online submissions for To Lower Case.

But what if you can’t call that method? looking again to the problem, simple solution would be just having two strings with all alphabet and replace it based on index, like the example down bellow:

class Solution {

    public static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";

    public String toLowerCase(String s) {
        final StringBuilder r = new StringBuilder();
        for(int i=0;i<s.length();i++){
            final char c = s.charAt(i);
            final int upperCaseIndex = UPPERCASE.indexOf(c);
            r.append(upperCaseIndex == -1 ? c : LOWERCASE.charAt(upperCaseIndex));
        }
        return r.toString();
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 1 ms, faster than 18.53% of Java online submissions for To Lower Case.

Memory Usage: 40.2 MB, less than 97.85% of Java online submissions for To Lower Case.

It works but there is so much that needs to be done in here, besides the iteration you have to grab index, compare and then append.

A much faster solution for this would be focusing on the character itself and how it works.

Characters can be replaced with numbers and each character represent a number as well, meaning you can cast a number to a character if needed, and the other as well.

char-table

From the image above the decimals ones are the ones that we are looking into it. If you compare uppercase letter A with lowercase a the difference is 32, so that will be our magic number.

So to get this sorted out in a quick way you can leverage from this information in your problem solution:

class Solution {
    public String toLowerCase(String s) {
        final StringBuilder builder = new StringBuilder();
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            // a to z to ignore the special characters
            // if(c < 'a') c = c + ' ';  // incompatible types: possible lossy conversion from int to char
            if(c >= 'A' && c <= 'Z') c = (char) (c + 32); 
            builder.append(c);
        }
        return builder.toString();
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 0 ms, faster than 100.00% of Java online submissions for To Lower Case.

Memory Usage: 40.7 MB, less than 34.30% of Java online submissions for To Lower Case.

Same iteration, with same builder, but now ignoring the special characters and focusing only on the characters that are needed and when an upper one is found we add 32 and cast to avoid lossy conversion and append into our builder, and done!


That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .