We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In C and C++, chars are represented as ints under the hood, so you can do stuff like int foo = 1 - 'a';.
Using this idea, they create an array of 26 elements -- one element for each letter of the alphabet -- then use a[c] - 'a' to calculate the index that the character a[c] belongs to.
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
In C and C++,
char
s are represented asint
s under the hood, so you can do stuff likeint foo = 1 - 'a';
.Using this idea, they create an array of 26 elements -- one element for each letter of the alphabet -- then use
a[c] - 'a'
to calculate the index that the charactera[c]
belongs to.Note that
'a' - 'a'
equals 0,'b' - 'a'
equals 1,'c' - 'a'
equals 2, ..., and'z' - 'a'
equals 25.So, if
first[1]
is 3, that means there are 3 "b"'s ina
, and ifsecond[25]
is 42, then that means there are 42 "z"'s inb
.