CharCount Array

Why charCount array is needed? 

When we want to keep track of the frequency of the character in an array or string, we need to store the count against the array element or character. 

In this case, array index is used to store the ASCII value of any char such as digits, alphabets, special characters, symbols. 

ASCII value of 'a' is 97, 'b' is 98, and goes on

ASCII value of 'A' is 65, 'B' is 66, and goes on

ASCII value of '0' is 48, '1' is 49, and goes on

For example, If we store the index as 'a', it will mean array[97] but not array['a']. Array's index should be only integer. 


When we try to get the frequency of each characters, it will work as below for the array "AABABBA"



Having 255 as the size is not always required. Some problems have the string only in lowercase letters. This requires just 26 size array, and this will sorted by doing like this. 



Click here for whiteboard link.

I always believed that count array is the rescue for most of the problems such as 




But, I know from the bottom of my heart, it takes some good amount of space from CPU. Most of the problems might use any of the 255 characters, so 255 ranged integer array was required all the time. 
Hence, the space complexity was always high in my programs. 

We can also use vectors, but somehow, the vectors took more space than array. 

  #define RANGE 255
  int count[RANGE] = {0};

  vector<int> v(RANGE, 0);

I still couldn't find any alternative, so this post is a placeholder and will be updated. 

Comments