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?

`m`

memory, if you are counting occurrences of`m`

different characters in a string?