To Lower Case
Problem Description​
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"
Complexity Analysis​
*** Time Complexity:**
*** Space Complexity:**
Constraints​
1 <= s.length <= 100
Solution for Kth Largest Element in a Stream
s
consists of printable ASCII characters.
Solution​
Approach​
The simple approach is use built in function to convert given string to lower case.
Code in Different Languages​
- C++
- Java
class Solution {
public:
string toLowerCase(string s) {
string result;
for (char c : s) {
if (c >= 'A' && c <= 'Z') {
result += (char)(c + 32);
} else {
result += c;
}
}
return result;
}
};
class Solution {
public:
string toLowerCase(string s) {
for (int i = 0; i < s.size(); ++i) {
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] = s[i] + ('a' - 'A');
}
}
return s;
}
};
Complexity Analysis​
Time Complexity:​
The toLowerCase method iterates through each character
c
in the input strings
. The iteration involves a single pass through the string, so the time complexity isO(n)
, wheren
is the length of the input strings
. Within each iteration, checking ifc
is an uppercase letter ('A' to 'Z') and converting it involves constant time operations (ord(c), chr()), which do not change the overall time complexity. Therefore, the time complexity of the toLowerCase method is O(n).
Space Complexity: ​
The method uses a list result to store the characters of the resulting lowercase string. Initially, an empty list result is created, which takes constant space. The space used by result grows as it accumulates characters from the input string s. Since the list result holds at most n characters (where n is the length of s), the space complexity is O(n). Therefore, the space complexity of the toLowerCase method is O(n).
References​
- LeetCode Problem: Kth Largest Element in a Stream