Sparse Array
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 2 instances of 'ab',1 of 'ab' and 0 of 'bc'. For each query, we add an element to our return array, result=[2,1,0] .
https://www.hackerrank.com/challenges/sparse-arrays/problem?isFullScreen=true
I tried this solution but it passes 3 test cases only.
static int[] matchingStrings(String[] strings, String[] queries) {
int[] result_arr=new int[queries.length];
HashMap<String,Integer> map=new HashMap<>();
for(int i=0;i<queries.length;i++)
{
map.put(queries[i],i);
}
for(int i=0;i<strings.length;i++)
{
if(map.containsKey(strings[i]))
{
int index=map.get(strings[i]);
System.out.println(index);
result_arr[index]+=1;
}
}
return result_arr;
}