Given a string which contains only letters. Sort it by lower case first and upper case second.
Have you met this question in a real interview?
Yes
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.
public class Solution {
/**
*@param chars: The letter array you should sort by Case
*@return: void
*/
public void sortLetters(char[] chars) {
//write your code here
if(chars == null || chars.length == 0) return;
int left = 0, right = chars.length - 1;
while(left < right){
while(left < right && Character.isLowerCase(chars[left]))left++;
while(left < right && !Character.isLowerCase(chars[right]))right--;
if(left < right){
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++; right--;
}
}
}
}
No comments:
Post a Comment