-
Notifications
You must be signed in to change notification settings - Fork 90
/
Sort Letters by Case.py
36 lines (28 loc) · 1.05 KB
/
Sort Letters by Case.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Given a string which contains only letters. Sort it by lower case first and upper case second.
Note
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"
"""
__author__ = 'Danyang'
class Solution:
def sortLetters(self, chars):
"""
General sort: O(n lg n)
But in this case, the possible elements are in a closed set - either lower case or upper case; thus O(n)
The array is abstracted to | closed set | open set |, and move all lower case letters into closed set.
:param chars: The letters array you should sort.
:return: NIL, in-place
"""
closed = -1
for ind, val in enumerate(chars):
if ord(val) < ord('a'): # ASCII A-Za-z
continue
else:
closed += 1
chars[ind], chars[closed] = chars[closed], chars[ind]
if __name__ == "__main__":
chars = list("abAcD")
Solution().sortLetters(chars)
assert "".join(chars) == "abcAD"