Sort Letters By Case
Question
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.
Thoughts
just like sort colors, only two type here
Solution
class Solution:
"""
@param chars: The letters array you should sort.
"""
def sortLetters(self, chars):
if not chars:
return None
# write your code here
numRed = 0
length = len(chars)
index = 0
while index <= length - 1:
if chars[index] in 'abcdefghijklmnopqrstuvwxyz':
chars = self.swap(chars, numRed, index)
numRed += 1
index += 1
else:
index += 1
return chars
def swap(self, chars, i, j):
tmp = chars[j]
chars[j] = chars[i]
chars[i] = tmp
return chars