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.
- Remove characters from the first string which are present in the second string
- Print list items containing all characters of a given word
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Print all permutations with repetition of characters
- Interleaving of two given strings with no common characters
- String matching where one string contains wildcard characters
- Find the first non-repeating character from a stream of characters
- Rearrange a string so that all same characters become d distance away
- Given a sorted dictionary of an alien language, find order of characters
- Find the longest substring with k unique characters in a given string
- Reverse a string without affecting special characters
- Find all strings formed from characters mapped to digits of a number
- In-place replace multiple occurrences of a pattern
- Rearrange a string so that all same characters become atleast d distance away