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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort ugly numbers in an array at their relative positions
- Alphanumeric Abbreviations of a String
- How to remove all non-alphanumeric characters from a string in Java
- How to check string is alphanumeric or not using Regular Expression
- Generate a string from an array of alphanumeric strings based on given conditions
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check if count of Alphabets and count of Numbers are equal in the given String
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Compare two strings considering only alphanumeric characters
- Count alphanumeric palindromes of length N
- Sort all special primes in their relative positions
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Find numbers of balancing positions in string
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Sub-string that contains all lowercase alphabets after performing the given operation
- Generate a string with maximum possible alphabets with odd frequencies
- Check if a string contains only alphabets in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.