Sparse Arrays [Hacker Rank]

Kim Hayeon·2023년 4월 18일
0

Algorithm Study

목록 보기
9/37
post-thumbnail

Question

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. Return an array of the results.

Function Description

Complete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in strings.

matchingStrings has the following parameters:

  • string strings[n] - an array of strings to search
  • string queries[q] - an array of query strings

Returns

  • int[q]: an array of results for each query

Input Format

The first line contains and integer , the size of .
Each of the next lines contains a string .
The next line contains , the size of .
Each of the next lines contains a string .

Constraints

1 <= n <= 1000
1 <= q <= 1000
1 <= |strings[i]|,|queries[i]| <= 20

풀이

def matchingStrings(strings, queries):
    s = strings
    q = queries
    answer = [0 for i in range(len(q))]
    for i in range(len(q)):
        for j in range(len(s)):
            if q[i] == s[j]:
                answer[i] += 1
        
    return answer

이것도 범위가 작아서 하나씩 비교해줬다.
queries[i]가 strings[i]와 같으면 갯수를 하나씩 더해줬다.

profile
우리는 무엇이든 될 수 있어

0개의 댓글