Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sparse Arrays in C++ HackerRank There is a collection of input strings and a col

ID: 3903431 • Letter: S

Question

Sparse Arrays in C++ HackerRank

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.

For example, given input strings = ['ab, 'ab', 'abc'], and queries = ['ab', 'abc','bc'] , we find  instances of 'ab',  of 'abc' and  of 'bc'. For each query, we add an element to our return array, returns = [2,1,0].

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 , 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



.

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

Source Code in C++

// Complete the matchingStrings function below.
vector matchingStrings(vector strings, vector queries) {


}

Please explain each line of code and please explain the logic behind each line of code. For example, explain why we need push_back(0) and why there is a 0 inside the parentheses.

Explanation / Answer

vector<int> matchingStrings(vector<string> strings, vector<string> queries) {

}

push_back(0) is appending value into vector to initilize of the word occurence, it is similar to vect[i] = 0, when using of push_back function it will increment index automatically...

let me know if you have any queries. Thank you...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote