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
#define RANGE 255 int count[RANGE] = {0}; vector<int> v(RANGE, 0);
Comments
Post a Comment