Given a string str of alphanumeric characters, the task is to sort the similar group of consecutive characters separately and print the modified string i.e. all consecutive groups of digits and alphabetical characters will be sorted separately.
Input: str = “121geeks21”
“121”, “geeks”, “21” are the valid groups
and they will be sorted separately.
Input: str = “cba321ab”
Approach: Create a vector to store the starting indices of all the valid groups in the given string. Now, traverse the string character by character and if the current character is from a different group than the previous character then push the current index to the vector. After the string has been traversed completely, sort all the individual groups of substring of the given string using the vector updated earlier. Finally, print the modified string.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Sort string of characters
- Sort a string according to the frequency of characters
- Sort string of characters using Stack
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Min flips of continuous characters to make all characters same in a string
- Bucket Sort To Sort an Array with Negative Numbers
- String with k distinct characters and no same characters adjacent
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Count the minimum number of groups formed in a string
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
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.