Sort Letters by Case
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.
Solution
/**
*@param chars: The letter array you should sort by Case
*@return: void
*/
public void sortLetters(char[] chars) {
if(chars == null || chars.length == 0)
return;
int lower = 0;
for(int i = 0; i < chars.length; i++) {
if(chars[i] - 'a' >= 0 && chars[i] - 'a' <= 26) {
if(i > lower) {
char tmp = chars[i];
chars[i--] = chars[lower];
chars[lower] = tmp;
}
lower++;
}
}
}