how to solve this question?
To analysing a string, then finding the char whose counts of appearing in string is max.
how to solve this question?
for example:
Code:
char *string = "ab---ebbbe";
the appearing counts of char 'b' is max. It is 4.
I now think two method.
one is :
Code:
sort(string);
statisticalCountofChar(string);
the other is
Code:
char *individualofChar;
for ( string )
{
for ( *individualofChar )
{
caculateCountofChar();
}
}
is there even more better method to solve this question?
how to solve this question using HASHING TABLE? how to design hashing function ?
Re: how to solve this question?
In hash table you can solve this requirement easily.
You can have some algorithm to convert the string key to some index value.
And you can pass the strings to that algorithm.
If you found that string you can increment the count of the characters as value of that index in hash array.
Then finally your hash should be like this.
Ex:
If you have ab---bbbc
hash[a] = 1
hash[b]= 4
hash[c]=1
If you want b count you can just print the value of hash[b].
Reference for HASH algorithm : http://www.burtleburtle.net/bob/hash/doobs.html
Re: how to solve this question?
Did you go through the link which I have given to you?
Actually, that link has more hash functions.
If that link is very hard to read try these URL's:
http://www.cse.yorku.ca/~oz/hash.html
How to write a hash function in C? - Stack Overflow
Eternally Confuzzled - Hash Table Tutorial
A Hash Function for Hash Table Lookup
Try the sample Hash function from K&R C Book:
Code:
#define HASHSIZE 101
unsigned hash(char *s)
{
unsigned hashval;
for(hashval=0;*s!='\0';s++)
hashval=*s+31*hashval;
return hashval%HASHSIZE;
}
You need keep some points while writing hash function,
* It should not have the collision(same key for different strings). If it has it should have the solution.
* And it should always return same key for the same inputs.
You can also develop your own hash algorithm for hash function.
I think After look on those URL's you will get a clear picture about it.
Re how to solve this question?
Actually, K&R used 31. Because it is an odd prime number. And in ASCII table up-to 31 characters are non-printable character.
And it will give less no of collisions.
And note that expression won't give result as 1-31 value for any character.
And refer this URL it is very interesting,
Why do hash functions use prime numbers? « Computing Life