Create a method solution that takes the parameters of the string s, sorts them in descending order, and returns a new string. s consists only of lowercase letters and uppercase letters, and uppercase letters are treated as values smaller than lowercase letters.
--Parameter: The length of s is a string of 1 or more.
x | result |
---|---|
"Zbcdefg" | "gfedcbZ" |
class Solution {
public String solution(String s) {
return Stream.of(s.split("")) //Split the string character by character
.sorted(Comparator.reverseOrder()) //Sort in descending order
.collect(Collectors.joining()); //Combine the divided character strings into one character string.
}
}
class Solution {
public String solution(String s) {
char[] sol = s.toCharArray(); //Get a char array from a string
Arrays.sort(sol); // "Zbcdefg"
//Use the StringBuilder reverse to reverse the order.
return new StringBuilder(new String(sol)).reverse().toString();
}
}
Recommended Posts