• + 1 comment

    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.