Given an alphanumeric string str, the task is to sort the string in such a way that if a position is occupied by an alphabet it must be occupied by an alphabet after sorting and if occupied by a number it must be occupied by a number after sorting.
Input: str = “geeks12for32geeks”
Input: str = “d4c3b2a1”
Approach: We will convert the string to a character array and then sort the character array c. After sorting the character array the numeric characters will occupy starting indices of the array and the alphabets will occupy the remaining part of the array.
The numeric half will be sorted and the alphabet part will also be sorted. We will keep two indices one at the starting index of the alphabet part al_c and one at the starting index of numeric part nu_c, now we will check the original string and if a position was occupied by an alphabet then we will replace it with c[al_c] and increment al_c else we will replace it with c[nu_c] and increment nu_c.
Below is the implementation of the above approach:
Time complexity: O(N * log(N)) where N is the length of the string.
- Sort ugly numbers in an array at their relative positions
- Find numbers of balancing positions in string
- Alphanumeric Abbreviations of a String
- Remove all characters other than alphabets from string
- Sub-string that contains all lowercase alphabets after performing the given operation
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java using Lambda expression
- Reverse a string preserving space positions
- Find time taken for signal to reach all positions in a string
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Bucket Sort To Sort an Array with Negative Numbers
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort a string according to the order defined by another string
- Sort 3 numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.