python – Number of occurrences of strings in array


This is a Hackerrank problem (https://www.hackerrank.com/challenges/sparse-arrays/problem).

We’re given an array of strings and an array of queries, and have to return an array of the number of occurrences of each query in the array of strings.
For example: s = (‘ab’, ‘abc’, ‘aba’, ‘ab’). q = (‘ab’, atr’, ‘abc). We need to return (2, 0, 1).

The constraints are 1 <= n <= 1000, 1 <= q <= 1000, where n and q are the number of strings and queries respectively, and 1 <= |strings(i)|, |queries(i)| <= 20.

Here is my code:

def matchingStrings(strings, queries):
    results = ()
    dic = {q:0 for q in queries}
    for s in strings:
        if s in dic: dic(s) += 1
    for q in queries:
        if q in dic: results.append(dic(q)) 
    return results

Is there anything I can optimize? Speed, memory usage? Anything else?