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.
jonathen, freq is the vector created. It is declared a size of 26 values, all set to 0.
The first two (for) loops go through a string, char by char and use the freq vector to keep track of each char's occurrence.
++freq is increasing the value of the vector at the index [c minus 'a'].
What you are seeing is ASCII interpretation for the index. Since 'a' is 97, if c was 'a', then the index would be [0] because 97-97. Same continues for 'b' which would be 98-97 indicating an index of [1].
cool_shark makes good use of one vector by using --freq to account for char matches between the two strings. Note that this is good programming practice instead of using nested loops which would have given a runtime of O(n^2) instead of its current linear runtime.
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
jonathen, freq is the vector created. It is declared a size of 26 values, all set to 0.
The first two (for) loops go through a string, char by char and use the freq vector to keep track of each char's occurrence.
++freq is increasing the value of the vector at the index [c minus 'a']. What you are seeing is ASCII interpretation for the index. Since 'a' is 97, if c was 'a', then the index would be [0] because 97-97. Same continues for 'b' which would be 98-97 indicating an index of [1].
cool_shark makes good use of one vector by using --freq to account for char matches between the two strings. Note that this is good programming practice instead of using nested loops which would have given a runtime of O(n^2) instead of its current linear runtime.