Input: BDCAbaxz
OUTPUT: AaBbCDxz
My solution is straight forward and ugly:
-
Sort the input with quick sort, then we get “ABCDabxz”
-
Alloc a temp array with the same size as the original one, then take the appropriate element from two sub-array(ptr1–>A, ptr2—>a)
-
Copy the temp array back to the original one
Any faster algorithm on this problem?
Yes.
Define a comparator for
qsortthat gives you the ordering you want in the first place (so rather than usingAB...YZab...yzas the sort order, have it enforceAaBb...YyZz).