Skip to main content

Sparse Arrays-HackerRank Solution

 Problem:

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.

Example


There are  instances of ' of '' and  of ''. For each query, add an element to the return array, .

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 .


Solution:

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

/*
 * Complete the 'matchingStrings' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. STRING_ARRAY strings
 *  2. STRING_ARRAY queries
 */

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

}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string strings_count_temp;
    getline(cin, strings_count_temp);

    int strings_count = stoi(ltrim(rtrim(strings_count_temp)));

    vector<string> strings(strings_count);

    for (int i = 0; i < strings_count; i++) {
        string strings_item;
        getline(cin, strings_item);

        strings[i] = strings_item;
    }

    string queries_count_temp;
    getline(cin, queries_count_temp);

    int queries_count = stoi(ltrim(rtrim(queries_count_temp)));

    vector<string> queries(queries_count);

    for (int i = 0; i < queries_count; i++) {
        string queries_item;
        getline(cin, queries_item);

        queries[i] = queries_item;
    }

    vector<int> res = matchingStrings(strings, queries);

    for (size_t i = 0; i < res.size(); i++) {
        fout << res[i];

        if (i != res.size() - 1) {
            fout << "\n";
        }
    }

    fout << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<intint>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<intint>(isspace))).base(),
        s.end()
    );

    return s;
}













Comments

Popular posts from this blog

full video source

TO GET MEGA STEPS TO ACCESS THE THE Mega  1) Open  the below link in your browser 👆You have to disable your adbloker  2) Select The Box "I Am Not A Robot" , if it asks  3) Wait for 5  seconds and click Free access with ads (dont join premium)                                                 CLICK HERE TO GET LINK    4) Click "Discover Articles" & Wait For 5 Seconds After That Click On The X  5) Click "Related Topics" & Wait For 5 Seconds After That Click On The X For the app download part,if it asks , click to download the app  and when redirected just go back and wait 1 minute you don’t have to download it ,after that minute you will see the tick mark  appear .(YOU DON'T HAVE TO DOWNLOAD ANYTHING)  6) Click On Continue That's it enjoy your mega link 👍 NOTE👉All links are FREE & NO SPAM. The reason you have to go through this is to support me and  prevent the website from getting banned. Thank you 😊.

FULL VIDEO SOURCE

  CLICK HERE  JESSA RHODES FULL COLLECTION         EMILY LYNN CLICK HERE LINK FULL VIDEO CLICK HERE TO GET HENTAI LINK STEPS TO ACCESS THE THE Mega 1) Open the link in your browser 👆You have to disable your adbloker 2) Select The Box "I Am Not A Robot" , if it asks 3) Wait for 5 seconds and click Free access with ads (dont join premium) 4) Click "Discover Articles" & Wait For 5 Seconds After That Click On The X 5) Click "Related Topics" & Wait For 5 Seconds After That Click On The X For the app download part,if it asks , click to download the app and when redirected just go back and wait 1 minute you don’t have to download it ,after that minute you will see the tick mark appear .(YOU DON'T HAVE TO DOWNLOAD ANYTHING) 6) Click On Continue That's it enjoy your mega link 👍 NOTE👉All links are FREE & NO SPAM. The reason you have to go through this is to support me and prevent the website from getting banned. Thank you 😊.

Reality Kings Complete Premium Collection – 29GB

                           FREE LINK BELOW  What is Mega? Mega is a cloud storage service comparable to Google Drive, Dropbox, and other similar services. Create an account, upload a file, receive a link to that file once it's been uploaded, then share that link with individuals who need it. If you receive a link from someone, simply click on it to begin downloading. (Just double-check that it's MEGA                                                    CLICK HERE FOR MEGA   How To get Mega Free And Exclusively? There are many ways to get free mega but I am going to tell you about something where you can get any movies or any anything for free : Step 1: you all must have heard about telegram .It has become favourite for people because here there are many channels that are providing free of cost movies link easily.First you have to download telegram form their website : TO download:   https://telegram.org/ Step 2: In this what you have to do is Search the name of movies ,channel or