I am looking for the most efficient (time and space) algorithm for character frequency calculation for a given string.
The simplest algorithm that comes to mind is to have a flag-array (size = number of different characters) you want to search and increment the counter for the corresponding index. This works in linear time. Only problem with this is the space requirement of the flag-array, which could go upto 256 if all ASCII characters are needed.
Is there a better algorithm, which could save on space/time?
If you use a hash table to store the counters, you need space proportional to the number of different characters in your string and you can still run the computation in linear time. It is easy to see that you cannot get better than linear time, since you need to look at each character at least once.
In practice however, if your string really only uses one byte to store a character (i.e. it is not Unicode) your "flag array" will only be something about 1 kb and thereby probably be the best shot since it doesn't have the (constant factor) time and space overhead of the hash table.