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.
I increment the freq for each char from first array and decrement for each char for second array.
The common chars therefore will cancel out.
The ones that aren't common will either have a positive or negative value.
When we add the absolute values we get the total number of different chars.
My solution in c / c++.
Using either arrays or unordered_map to store the freqeuncy:
//std::unordered_map freq;
const int range = 'z' - 'a' + 1;
int freq[range] = {0};
Making Anagrams
You are viewing a single comment's thread. Return to all comments →
I increment the freq for each char from first array and decrement for each char for second array. The common chars therefore will cancel out. The ones that aren't common will either have a positive or negative value. When we add the absolute values we get the total number of different chars. My solution in c / c++. Using either arrays or unordered_map to store the freqeuncy:
//std::unordered_map freq; const int range = 'z' - 'a' + 1; int freq[range] = {0};