LeetCode / To Lower Case

LeetCode / To Lower Case

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public String toLowerCase(String str) {
        char[] charArry = str.toCharArray();
        for (int i = 0; i < charArry.length; i++) {
            charArry[i] = getLower(charArry[i]);
        }
        return new String(charArry);
    }
    
    private boolean isUpper(char c) {
        return (c >= 'A') && (c <= 'Z');
    }
    
    private char getLower(char c) {
        if (isUpper(c)) {
            return (char)((c - 'A') + 'a');
        } else {
            return c;
        }
    }
}
Solution 1
  • Description
    • Check each character from the beginning of the string, and convert to lowercase if it’s uppercase
  • Time Complexity
    • O(len(str))
    • For loop of size len(str)
  • Space Complexity
    • O(len(str))
    • Memory usage proportional to len(str) for function input