|
|
|
|
![]() ![]() |
Nov 13 2007, 10:40 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 346 Joined: 1-October 07 From: India Member No.: 50,968 myCENT:55.24 |
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 |
|
|
|
Nov 14 2007, 05:50 PM
Post
#2
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 936 Joined: 14-April 05 From: West Chester, PA Member No.: 5,636 |
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). |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 1st December 2008 - 06:00 PM |