DEV Community

loading...
Cover image for Solution: To Lower Case

Solution: To Lower Case

seanpgallivan
Fledgling software developer; the struggle is a Rational Approximation.
・2 min read

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.


Leetcode Problem #709 (Easy): To Lower Case


Description:


(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.


Examples:

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.

Idea:


(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)

The uppercase letters from 'A' to 'Z' have ASCCII codes between 65 and 90. We can iterate through our input string (s) and as we build up our answer string (ans), if the value (n) of any character lies in that range, we can replace it with the character of value n + 32, which corresponds to the lowercase version of the uppercase letter.

  • Time Complexity: O(N) where N is the length of s
  • Space Complexity: O(1) excluding the space of the output

Javascript Code:


(Jump to: Problem Description || Solution Idea)

var toLowerCase = function(s) {
    let ans = ""
    for (let c of s) {
        let n = c.charCodeAt()
        ans += n > 64 && n < 91 ? String.fromCharCode(n + 32) : c 
    }
    return ans
};
Enter fullscreen mode Exit fullscreen mode

Python Code:


(Jump to: Problem Description || Solution Idea)

class Solution:
    def toLowerCase(self, s: str) -> str:
        ans = ""
        for c in s:
            n = ord(c)
            ans += chr(n+32) if n > 64 and n < 91 else c
        return ans
Enter fullscreen mode Exit fullscreen mode

Java Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
    public String toLowerCase(String s) {
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            ans.append(c > 64 && c < 91 ? (char)(c + 32) : c);
        }
        return new String(ans);
    }
}
Enter fullscreen mode Exit fullscreen mode

C++ Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
public:
    string toLowerCase(string s) {
        string ans = "";
        for (auto& c : s)
            ans += c > 64 && c < 91 ? c + 32 : c;
        return ans;
    }
};
Enter fullscreen mode Exit fullscreen mode

Discussion (4)

Collapse
10xlearner profile image
10x learner

In Modern C++, you can actually do that more efficiently and in a more readable code:

class Solution {
public:
    string toLowerCase(string s) {
        std::transform(s.begin(), s.end(), s.begin(),
            [](const auto& c){ return std::tolower(c); }
        );
        return s;
    }
];
Enter fullscreen mode Exit fullscreen mode

If you want to know more about them, here are some links to std::transform documentation and std::tolower 😉

Collapse
seanpgallivan profile image
seanpgallivan Author

That misses the point of the exercise, though. Almost all languages have some kind of toLower method built-in. The exercise is to see if we can write our own code to do the same thing that the built-in methods do, to help us understand the process behind-the-scenes.

I mean, Javascript and Java have s.toLowerCase() and Python has s.lower(), all of which directly provide the solution.

Also, for C++, you can omit the explicit lambda from your example and just directly apply the standard function:

class Solution {
public:
    string toLowerCase(string s) {
        transform(s.begin(), s.end(), s.begin(), ::tolower);
        return s;
    }
];
Enter fullscreen mode Exit fullscreen mode

And there's also the simple iteration:

class Solution {
public:
    string toLowerCase(string s) {
        for (auto& c : s) c = tolower(c);
        return s;
    }
];
Enter fullscreen mode Exit fullscreen mode
Collapse
10xlearner profile image
10x learner

Indeed, now that you've pointed it out, with the purpose of understanding how tolower functions of programming languages works, your solutions are clearly more appropriate ! 👍

Nice point on the C++ example ! I always forget that we can pass functions like that, without the explicit lambda! 😄 👌

Maybe, it would have been interesting to mention the tolower of the language you used, directly in your article, for people to learn about them if they didn't.
In my little experience as a software engineer, people tends to re-write that kind of function each time they need it, even if they already exists and are available in their language, only because they don't know about them. I would be very interested to know if you had some similar experiences about that ? 😃

Collapse
hanpari profile image
Pavel Morava • Edited

Only the Java solution seems fully correct since it uses StringBuilder. The others are probably suboptimal. Curiously enough, I would guess that even your C++ was slower than Java for bigger Ns, but I am not an expert in C++.

What the other solutions do is that they create a fixed array of characters each time you add a new character to it.

In Python, you should create a list, use its append method, and then "".join(list). This is equivalent to StringBuilder in C# or Java. I guess Javascript works similarly.

Futhermore, in Python you can use 1 < x < 5 construct instead 1 < x and x < 5.