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.
First string: “a”
Second string: “b, b”
Our desired answer would be 3.
However, if we use the second for loop above, our map would look like:
< a, 1 > , < b, 0 >
We would get an answer of 1.
This is incorrect because he added count.put(c, 1).
When we see the first “b”, we add “b” into the map( < b, 1 >). On the second iteration, since “b” already exists as a key in the hash table, we delete the value by 1, which gives us 0.
As Ndel mentioned, the fix would be to change it into count.put(c, -1) and sum the absolute values.
Hope that helps, Cheers
Cookie support is required to access HackerRank
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 →
Hi Antrikshverma2,
That’s right – you have the right idea of what the hash table is supposed to do.
However, in susmithamenda’s example, he did:
It is possible that we get an example such as:
First string: “a” Second string: “b, b” Our desired answer would be 3.
However, if we use the second for loop above, our map would look like:
< a, 1 > , < b, 0 > We would get an answer of 1.
This is incorrect because he added count.put(c, 1).
When we see the first “b”, we add “b” into the map( < b, 1 >). On the second iteration, since “b” already exists as a key in the hash table, we delete the value by 1, which gives us 0.
As Ndel mentioned, the fix would be to change it into count.put(c, -1) and sum the absolute values.
Hope that helps, Cheers