709. To Lower Case

Description of the Problem

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.

Solution

Code (C++)

class Solution {
public:
    string toLowerCase(string s) {
        for(int i = 0; i < s.size(); i++){
            char c = s[i];
            if ('A' <= c && c <= 'Z'){
                s[i] = c - 'A' + 'a';
            }
        }
        return s;
    }
};

Complexity

  • n is length of string s

Time Complexity:

  • \(T(n) = \Theta(n)\)

Auxiliary Space:

  • \(S(n) = O(1)\)