Skip to content

Commit

Permalink
Create sparse array
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajwadhwa authored Oct 20, 2016
1 parent 0ede745 commit 1f7612e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions sparse array
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int noOfStrings = scan.nextInt();
//scan.nextLine();
Map<String, Integer> hashMap = new HashMap<>();

for(int i = 0; i < noOfStrings; i++){
String s = scan.next();
if(hashMap.containsKey(s))
hashMap.put(s, hashMap.get(s) + 1);
else
hashMap.put(s, 1);
scan.nextLine();
}


int noOfQueries = scan.nextInt();
scan.nextLine();
//System.out.print(noOfQueries);
// for(int i = 0; i < noOfQueries; i++){
// String s= scan.nextLine();
// System.out.println(s);
//}

for(int i = 0; i < noOfQueries; i++){
String s = scan.nextLine();
if(hashMap.containsKey(s)){
System.out.println(hashMap.get(s));
}
else
System.out.println(0);
}

}
}

0 comments on commit 1f7612e

Please sign in to comment.