Sparse Arrays HackerRank

1.USING ARRAYLIST


import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



public class Solution {
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String str[]=new String[n];
        ArrayList<String> aa=new ArrayList<String>();
        
        for(int i=0;i<n;i++){
            str[i]=sc.next();
            aa.add(str[i]);
        }
        
        
        int q=sc.nextInt();
        String qry[]=new String[q];
        for(int i=0;i<q;i++){
            qry[i]=sc.next();
        }
        
        int index=0;
        int f[]=new int[qry.length];
        
        for(int i=0;i<q;i++){
            
            f[index++]=Collections.frequency(aa,qry[i]);
            
        }
        
        for(int i=0;i<f.length;i++){
            System.out.println(f[i]+" ");
        }
              
    }
}


/*TIME O(N)
SPACE O(N) using additional array and converting given string array to an ArrayList.

We add elements of a given string array to an ArrayList.
Then we iterate a query loop and store the frequency of each element in the f array
after seeing its occurrence in the ArrayList.

*/

<-------------------------------------------------------------------------------->

2.USING HASHMAP

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class Solution {
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String str[]=new String[n];
        HashMap<String,Integer> aa=new HashMap<String,Integer>();
        
        for(int i=0;i<n;i++){
            str[i]=sc.next();
            
        }
        
        for(int i=0;i<n;i++){
            if(aa.containsKey(str[i])){
                aa.put(str[i],aa.get(str[i])+1);
            }
            else
                aa.put(str[i],1);
        }
            
        
        int q=sc.nextInt();
        String qry[]=new String[q];
        for(int i=0;i<q;i++){
            qry[i]=sc.next();
        }
        
        int index=0;
        int f[]=new int[qry.length];
        
        for(int i=0;i<q;i++){
            if(aa.containsKey(qry[i])){
                f[index++]=aa.get(qry[i]);
            }
            else{
                f[index++]=0;
            }
        }
        
        for(int i=0;i<f.length;i++){
            System.out.println(f[i]);
        }
        
    }
}

/*TIME O(N)
SPACE O(N) using hashmap
We will first store the string(from the string array) as key and their frequency as values in the hashmap.
Then we will iterate the query array and if we found a match with the hashmap key element
then we will store the value(which is the frequency of key ) in the f array.
*/


Comments

Popular posts from this blog

Java Date And Time HackerRank

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024

Minimum Indices HackerEarth