DEV Community

loading...
Cover image for Sparse Arrays Code Challenge Solved

Sparse Arrays Code Challenge Solved

rezehnde profile image Marcos Rezende ・1 min read

Ok! That's a simple problem with a simple solution written in Python 3, but to not lose the habit, I am publicly exposing my solution here.

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.

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:

  • strings - an array of strings to search
  • queries - an array of query strings

Solution

def matchingStrings(strings, queries):
    return [strings.count(query) for query in queries]
Enter fullscreen mode Exit fullscreen mode

That is it. With one single line, we are returning an array that counts how many queries appears in strings.

Discussion (0)

Forem Open with the Forem app