Given a string of characters (let us say there are about 100 characters or more in the string), what is the most efficient method to use for finding out the character that repeats itself the most?Is it possible for us to use some kind of data structure for this?, if this is so,which is the best one that needs to be used?
Total Answers and Comments: 6
Last Update: October 23, 2009 Asked by: jobchicago
RE: Data Structure - character that repeats itself
Th solution I can think of is of complexity O(n) by using an additional array of size 26.
Scan the entire array and increase the counter for the respective characters. After the scan is done we need another scan of those counters to find the maximum one and that will be the chanracter with maximum frequency. this will also take O(n) time.
I am eager to a get solution better than this..:-)
RE: Data Structure - character that repeats itself
I have an opinion that a hash table with indexing based on the ascii values of the characters would bring the complexity down further and would add some time benefits as well.
RE: Data Structure - character that repeats itself
There is an way to solve this issue with complexity of O(n).
1. MAke an array size of 256
2. Using each charater read from the string as an index incremented the counter of corresponding character.
3. Let that counter be max so inshort you have variables holding a char with number of times repeated in the string.
4. Repeat the proceess for all the characters and compare it with current max counter if maximum change it
There might be posibility of more than one charater haivng same max counter problem don't ask for such situation any way for an interview keep it precise guys!!