Sakshi Singh
1 min readDec 27, 2018

HACKERRANK SOLUTION: SPARSE ARRAYS

import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;

import java.util.regex.*;

public class Solution {

// Complete the matchingStrings function below.

static int[] matchingStrings(String[] strings, String[] queries) {

int n = queries.length;

int[] ar = new int[n];

int l = strings.length;

for(int i=0; i<n; i++)

{

ar[i] =0;

for(int j=0; j<l; j++)

{

if(queries[i].equalsIgnoreCase(strings[j]))

ar[i]++;

}

}

return ar;

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv(“OUTPUT_PATH”)));

int stringsCount = scanner.nextInt();

scanner.skip(“(\r\n|[\n\r\u2028\u2029\u0085])?”);

String[] strings = new String[stringsCount];

for (int i = 0; i < stringsCount; i++) {

String stringsItem = scanner.nextLine();

strings[i] = stringsItem;

}

int queriesCount = scanner.nextInt();

scanner.skip(“(\r\n|[\n\r\u2028\u2029\u0085])?”);

String[] queries = new String[queriesCount];

for (int i = 0; i < queriesCount; i++) {

String queriesItem = scanner.nextLine();

queries[i] = queriesItem;

}

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

for (int i = 0; i < res.length; i++) {

bufferedWriter.write(String.valueOf(res[i]));

if (i != res.length — 1) {

bufferedWriter.write(“\n”);

}

}

bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();

}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sakshi Singh
Sakshi Singh

No responses yet

Write a response