sparse arrays hackerrank solution in python

Solutions on MaxInterview for sparse arrays hackerrank solution in python by the best coders in the world

showing results for - "sparse arrays hackerrank solution in python"
Charmaine
31 Jun 2018
1from collections import Counter # module to count the values
2#write your code here
3store = Counter(strings)
4ans = []
5for q in queries:
6  ans.append(store[q])
7return ans  
Leora
15 Jun 2020
1store = dict()
2    ans = []
3    
4    for w in strings:
5        if w in store:
6            store[w] +=1    #adding value to the key word(current word in string)
7        else:
8            store[w]=1      #assigning 1 to the new word in store
9            
10    for q in queries:
11        if q in store:
12            ans.append(store[q])
13        else:
14            ans.append(0)
15    return ans