Sort Letters by Case
Source
Given a string which contains only letters. Sort it by lower case first and upper
case second.
Example
For "abAcD", a reasonable answer is "acbAD"
Note
It's NOT necessary to keep the original order of lower-case letters and upper case
letters.
Challenge
Do it in one-pass and in-place.
Java
public class Solution {
public void sortLetters(char[] chars) {
int n = chars.length;
int start=0;
int end = n-1;
while(start<=end){
if(Character.isLowerCase(chars[start])){
start++;
}
else{
swap(chars, start, end);
end--;
}
}
}
public void swap(char[] chars, int start, int end){
char tmp = chars[start];
chars[start] = chars[end];
chars[end] = tmp;
}
}