You are viewing a single comment's thread. Return to all comments →
He created an array with size 26, by default the value is 0 for all the indices of the array.
here, "lettercounts[c-'a']++";
let's assume char value of c = 'e'.
The ascii value of 'e' is 101
The ascii value of 'a' is 97
So, the above line of code becomes,
lettercounts[101-97]++;
==> lettercounts[4]++;
// the above code is equivalent to
// lettercounts[4] = lettercounts[4] + 1;
So, if you print lettercounts[4], it prints 1, and with subsequent iterations the count gets incremented.
try it out with a simple int array
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
He created an array with size 26, by default the value is 0 for all the indices of the array.
here, "lettercounts[c-'a']++";
let's assume char value of c = 'e'.
The ascii value of 'e' is 101
The ascii value of 'a' is 97
So, the above line of code becomes,
lettercounts[101-97]++;
==> lettercounts[4]++;
// the above code is equivalent to
// lettercounts[4] = lettercounts[4] + 1;
So, if you print lettercounts[4], it prints 1, and with subsequent iterations the count gets incremented.
try it out with a simple int array