We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my easy understandable solution in C++, just ask gpt to do formatting of code (if not presentable).
include
using namespace std;
void matching_func(string str_list[], int str_size, string que_list[],
int que_size)
{ // this function will match the queries and str indexes and
// also directly print the results.
int result;
for(int q_index = 0; q_index < que_size; q_index++)
{ result = 0; // to make results 0 for each query start
for(int str_index = 0; str_index < str_size; str_index++)
{
// this inner loop will match specific query index element to string index elements.
if(que_list[q_index] == str_list[str_index])
{
result++; // if matched, then only increase the value
}
}
cout << result << endl;
}
}
void input_arr(int size, string arr[])
{
for (int index = 0; index < size; index++)
{
cin >> arr[index];
}
}
int main()
{
int size_string_list, size_query_list;
cin >> size_string_list;
string string_list[size_string_list];
input_arr(size_string_list, string_list);
cin >> size_query_list;
string query_list[size_query_list];
input_arr(size_query_list, query_list);
matching_func(string_list, size_string_list, query_list, size_query_list);
// here we have just taken the input of size and list
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
Here's my easy understandable solution in C++, just ask gpt to do formatting of code (if not presentable).
include
using namespace std; void matching_func(string str_list[], int str_size, string que_list[], int que_size) { // this function will match the queries and str indexes and // also directly print the results. int result; for(int q_index = 0; q_index < que_size; q_index++) { result = 0; // to make results 0 for each query start for(int str_index = 0; str_index < str_size; str_index++) {
// this inner loop will match specific query index element to string index elements. if(que_list[q_index] == str_list[str_index]) { result++; // if matched, then only increase the value } } cout << result << endl; } } void input_arr(int size, string arr[]) { for (int index = 0; index < size; index++) { cin >> arr[index]; } } int main() { int size_string_list, size_query_list; cin >> size_string_list; string string_list[size_string_list]; input_arr(size_string_list, string_list); cin >> size_query_list; string query_list[size_query_list]; input_arr(size_query_list, query_list); matching_func(string_list, size_string_list, query_list, size_query_list); // here we have just taken the input of size and list return 0; }