Strings: Making Anagrams

  • + 2 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.