-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsparse_array.py
More file actions
31 lines (25 loc) · 991 Bytes
/
sparse_array.py
File metadata and controls
31 lines (25 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
There is a collection of input strings and a collection of query strings.
For each query string, determine how many times it occurs in the list of input strings.
Example given a input strings = ['ab', 'ab', 'abc'] and
queries = ['ab',' abc', 'bc'] we find 2 instances of 'ab',
1 of 'abc' and 0 of 'bc'. For each query we add an element to our return array
results = [2, 1, 0]
Input Format
The first line contains and integer 'n', the size of strings.
Each of the next 'n' lines contains a string 'strings[i]'.
The next line contains 'q', the size of queries.
Each of the next 'q' lines contains a string 'q[i]'.
"""
def matching_strings(strings, queries):
results = []
for i in range(len(queries)):
count = 0
for j in range(len(strings)):
if strings[j] == queries[i]:
count += 1
results.append(count)
return results
strings = ['ab', 'ab', 'abc']
queries = ['ab','abc', 'bc']
print(matching_strings(strings, queries))