Game development as an Art

Sato Game Dev

Code Exercises


Sparse Arrays


Source: Hacker Rank

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.

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:
- strings - an array of strings to search
- queries - an array of query strings


Input Format
The first line contains and integer n, the size of strings.
Each of the next 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]

Constraints
1 <= n <= 1000
1 <= q <= 1000
1 <= |strings[i]|, |queries[i]| <= 20 Output Format
Return an integer array of the results of all queries in order.

Sample Input 1

4
aba
baba
aba
xzxb
3
aba
xzxb
ab

Sample Output 1
2
1
0
Explanation 1
Here, "aba" occurs twice, in the first and third string. The string "xzxb" occurs once in the fourth string, and "ab" does not occur at all.

Sample Input 2
3
def
de
fgh
3
de
lmn
fgh

Sample Output 2
1
0
1

Sample Input 3
13
abcde
sdaklfj
asdjf
na
basdn
sdaklfj
asdjf
na
asdjf
na
basdn
sdaklfj
asdjf
5
abcde
sdaklfj
asdjf
na
basdn

Sample Output 3

1
3
4
3
2

My solution:
C#:
 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{

    static int[] matchingStrings(string[] strings, string[] queries)
    {
        int[] queriesFound = new int[queries.Length];

        int queryNum = 0;
        foreach (string queriesComp in queries)
            {
            foreach (string stringsComp in strings)
            {
                if(stringsComp.Equals(queriesComp))
                {
                    queriesFound[queryNum]++;
                }

            }
            queryNum++;
        }
        
        return queriesFound;

    }

    static void Main(string[] args)
    {

        int stringsCount = Convert.ToInt32(Console.ReadLine()); //read num strings

        string[] strings = new string[stringsCount]; //create strings

        for (int i = 0; i < stringsCount; i++)
        {
            string stringsItem = Console.ReadLine();
            strings[i] = stringsItem;
        }

        int queriesCount = Convert.ToInt32(Console.ReadLine()); //read num queries

        string[] queries = new string[queriesCount];

        for (int i = 0; i < queriesCount; i++)
        {
            string queriesItem = Console.ReadLine();
            queries[i] = queriesItem;
        }

        int[] res = matchingStrings(strings, queries);

        //printing result into output
        foreach (int resOut in res)
        {
            Console.WriteLine(resOut);
        }

        Console.ReadLine();
    }
}