It is done using Counting sort
def case_specific_sort(s):
# Initialize the count arrays for lowercase and uppercase characters
lower_count = [0] * 26
upper_count = [0] * 26
# Count the occurrence of each lowercase and uppercase character
for char in s:
if char.islower():
lower_count[ord(char) - ord('a')] += 1
else:
upper_count[ord(char) - ord('A')] += 1
# Combine the sorted lowercase and uppercase characters
sorted_chars = []
for i in range(26):
sorted_chars.extend([chr(ord('a') + i)] * lower_count[i])
sorted_chars.extend([chr(ord('A') + i)] * upper_count[i])
# Convert the list of characters back to a string
sorted_string = "".join(sorted_chars)
return sorted_string
s = "hELLOwORLD"
sorted_string = case_specific_sort(s)
print(sorted_string) # Output: ehllloorDW