You are viewing a single comment's thread. Return to all comments →
Your solution runs in O(n*q). It is possible to do O(n+q). Here is a faster algorithm.
Create a HashMap<String,Integer> for each string in the first set if the map contains the string increment the associated int else put it in the map associated with 1 for each string in the second set if the map contains the string print the associated int else print 0
I believe this also could be implemented in two lines of code, but that's not my style. I used 7, thought it easier to read.
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
Your solution runs in O(n*q). It is possible to do O(n+q). Here is a faster algorithm.
I believe this also could be implemented in two lines of code, but that's not my style. I used 7, thought it easier to read.