Sort Letters

Given a string which contains only letters. Sort it by lower case first and upper case second.

Notice

It's NOT necessary to keep the original order of lower-case letters and upper case letters.

Example

For "abAcD", a reasonable answer is "acbAD"

Notes:

  1. Quick Sort partition
  2. Use Template
    public void sortLetters(char[] chars) {
        if(chars == null || chars.length == 0) {
            return;
        }

        int i = 0;
        int j = chars.length-1;
        char temp = chars[0];
        while(i < j) {
            while(i < j && Character.isUpperCase(chars[j])) {
                j--;
            }
            chars[i] = chars[j];

            while(i < j && Character.isLowerCase(chars[i])) {
                i++;
            }
            chars[j] = chars[i];
        }

        chars[i] = temp;
    }

results matching ""

    No results matching ""