709 To Lower Case
Given a string s
, return the string after replacing every uppercase letter with the same lowercase letter.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= s.length <= 100
s
consists of printable ASCII characters.
class Solution {
public String toLowerCase(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++){
if (chars[i] >= 'A' && chars[i] <= 'Z' ){
chars[i] = (char) (chars[i] - 'A' + 'a');
}
}
return new String(chars);
}
}
HashMap
Algorithm
- Build hashmap uppercase letter --> lowercase letter.
- Parse the string. If the current character is an uppercase letter, i.e. it's in the hashmap, then replace it by the hashmap value. Otherwise, keep it unchanged.
Implementation
class Solution {
public String toLowerCase(String s) {
Map<Character, Character> map = new HashMap<Character, Character>();
String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCase = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 26; i++){
map.put(upperCase.charAt(i), lowerCase.charAt(i));
}
for (int j = 0; j < s.length(); j++){
if (map.containsKey(s.charAt(j))){
sb.append(map.get(s.charAt(j)));
}else{
sb.append(s.charAt(j));
}
}
return sb.toString();
}
}
/*
hashmap
<Uppercase, lowercaese>
sb
*/
Complexity Analysis
- Time complexity: O(N) to parse the input string.
- Space complexity: O(N) to keep the output.