Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Sort characters in a string using merge sort

Implement merge sort to sort the characters of a string and return the sorted string.

Python practice14 minSearching & SortingIntermediateLast updated March 25, 2026

Problem statement

Given a string, sort its characters using the merge sort algorithm and return a new string with characters in non-decreasing order. Treat characters according to their natural Python ordering (i.e., uppercase letters come before lowercase letters by ASCII). Do not use built-in sorted().

Task

Use merge sort to sort the characters of a string and return the resulting string in non-decreasing character order.

Examples

Lowercase characters

Input

merge_sort_string('dbca')

Output

abcd

Characters of the string are sorted using merge sort to produce 'abcd'.

Input format

A single string s.

Output format

A string containing the characters of s sorted in non-decreasing order.

Constraints

- Do not use sorted() or str.sort(). - Use merge sort to achieve O(n log n) time complexity. - Preserve characters; only their order changes.

Samples

Sample 1

Input

'bbaac'

Output

aabbc

Duplicates are preserved and placed in sorted order.