Given a string of lowercase characters, the task is to print the string in a manner such that a character comes first in string displays first with all its occurrences in string.
Input : str = "geeksforgeeks" Output: ggeeeekkssfor Explanation: In the given string 'g' comes first and occurs 2 times so it is printed first Then 'e' comes in this string and 4 times so it gets printed. Similarly remaining string is printed. Input : str = "occurrence" output : occcurreen Input : str = "cdab" Output : cdab
This problem is a string version of following problem for array of integers.
Since given strings have only 26 possible characters, it is easier to implement for strings.
1- Count the occurrence of all the characters in given string using an array of size 26.
2- Then start traversing the string. Print every character its count times.
This article is contributed by Sahil Chhabra. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Group words with same set of characters
- Group consecutive characters of same type in a string
- Swap all occurrences of two characters to get lexicographically smallest string
- Minimum swaps to group similar characters side by side?
- Check If every group of a's is followed by a group of b's of same length
- Repeated Character Whose First Appearance is Leftmost
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum number of operations to move all uppercase characters before all lower case characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count of ungrouped characters after dividing a string into K groups of distinct 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
- Minimum characters to be replaced to make frequency of all characters same
- Strings formed from given characters without any consecutive repeating characters
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Longest substring with atmost K characters from the given set of characters
- String with k distinct characters and no same characters adjacent
- Group Shifted String
- Count Occurrences of Anagrams
- Check if all occurrences of a character appear together