Solution: To Lower Case
Tags: algorithms, javascript, java, python
Published: 2021-05-24 08:08:41

*This is part of a series of Leetcode solution explanations ([index](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you liked this solution or found it useful,* ***please like*** *this post and/or* ***upvote*** *[my solution post on Leetcode's forums](https://leetcode.com/problems/to-lower-case/discuss/1227317).*

---

#### [Leetcode Problem #709 (*Easy*): To Lower Case](https://leetcode.com/problems/to-lower-case/)

---

#### ***Description:***

<br />(*Jump to*: [*Solution Idea*](#idea) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

>
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:***

<br />(*Jump to*: [*Problem Description*](#description) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

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:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```javascript
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
};
```

---

#### ***Python Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```python
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
```

---

#### ***Java Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```java
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);
    }
}
```

---

#### ***C++ Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```c++
class Solution {
public:
    string toLowerCase(string s) {
        string ans = "";
        for (auto& c : s)
            ans += c > 64 && c < 91 ? c + 32 : c;
        return ans;
    }
};
```