xisto Community

# Data Structures -- String -- Arrange Based On Repetition String data structure

## Recommended Posts

Consider a string with any number of elements occurring any number of time.

Rearrange the string in such a way that the alphabet with most occurrence occurs in the followed by the next most occurring alphabet and so on... It should also be seen that the alphabets should occur frequency number of times.

Example:

Input : abcdaeghzabcdbhb

Output : bbbbaaaccddhhegz

##### Share on other sites

First of all, why are you posting a problem and not a solution or tutorial on how to find the solution? Your current post is just stupid since it is hard to respond to and it just looks like you are trying to get credits in any possible way.However, to solve this (since you did not say) is really easy. You can do it in O(n) and there are multiple ways to achieve this. The easiest method is O(n^2) or O(nlogn) depending on the sort method used. Since you know the size of the alphabet so you can initilize counts to zero. Then go through the string incrementing the letter's value by 1. Then just sort the list and print out the corresponding letter's and their counts. To do this in O(n), you would have to be kinda of tricky with how you store and sort. What you would do is obtain a letter's value and concatenate that number of them to the correct part of the out string (which you would keep track of with another string of values or an array).

##### Share on other sites
Rearrange the string in such a way that the alphabet with most occurrence occurs in the followed by the next most occurring alphabet and so on... It should also be seen that the alphabets should occur frequency number of times.Data Structures -- String -- Arrange Based On Repetition

Rearrange the string in such a way that the alphabet with most occurrence occurs in the followed by the next most occurring alphabet and so on... It should also be seen that the alphabets should occur frequency number of times.

-question by Abram

## Create an account

Register a new account