Given a string which contains only letters. Sort it by lower case first and upper case second.
java
public class Solution {
/*
* @param chars: The letter array you should sort by Case
* @return: nothing
*/
public void sortLetters(char[] chars) {
// write your code here
if (chars == null || chars.length == 0 || chars.length == 1) {
return;
}
int left = 0;
int right = chars.length - 1;
while (left <= right) {
while (left <= right && chars[left] >= 'a' && chars[left] <= 'z') {
left++;
}
while (left <= right && chars[right] >= 'A' && chars[right] <= 'Z') {
right--;
}
if (left <= right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
}
}
python
class Solution:
"""
@param: chars: The letter array you should sort by Case
@return: nothing
"""
def sortLetters(self, chars):
# write your code here
if chars is None or len(chars) == 0 or len(chars) == 1:
return
left, right = 0, len(chars) - 1
while left <= right:
while left <= right and chars[left].islower():
left += 1
while left <= right and chars[right].isupper():
right -= 1
if left <= right:
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1