@@ -0,0 +1,1728 @@
+package data_structures.arrays;%0D%0A%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.Scanner;%0D%0A%0D%0A/*%0D%0A * There are N strings. Each string's length is no more than 20 characters.%0D%0A * There are also Q queries. For each query, you are given a string, and you%0D%0A * need to find out how many times this string occurred previously.%0D%0A * %0D%0A * Input Format: %0D%0A * The first line contains N, the number of strings.%0D%0A * The next N lines each contain a string.%0D%0A * The N+2nd line contains Q, the number of queries.%0D%0A * The following Q lines each contain a query string.%0D%0A * %0D%0A * Constraints: %0D%0A * 1 %3C= N %3C= 1000%0D%0A * 1 %3C= Q %3C= 1000%0D%0A * 1 %3C= length of any string %3C= 20%0D%0A * %0D%0A * Sample Input: %0D%0A * 4%0D%0A * aba%0D%0A * baba%0D%0A * aba%0D%0A * xzxb%0D%0A * 3%0D%0A * aba%0D%0A * xzxb%0D%0A * ab%0D%0A * %0D%0A * Sample Output: %0D%0A * 2%0D%0A * 1%0D%0A * 0%0D%0A */%0D%0Apublic class SparseArrays %7B%0D%0A%0D%0A    public static void main(String%5B%5D args) %7B%0D%0A        %0D%0A        Scanner sc = new Scanner(System.in);%0D%0A        HashMap%3CString, Integer%3E map = new HashMap%3C%3E();%0D%0A        %0D%0A        int numStrings = Integer.parseInt(sc.nextLine());%0D%0A        for (int i = 0; i %3C numStrings; i++) %7B%0D%0A            String str = sc.nextLine();%0D%0A%0D%0A            if (!map.containsKey(str)) %7B%0D%0A                map.put(str, 1);%0D%0A            %7D else %7B%0D%0A                int count = map.get(str);%0D%0A                map.replace(str, count, count + 1);%0D%0A            %7D%0D%0A        %7D%0D%0A        %0D%0A        int numQueries = Integer.parseInt(sc.nextLine());%0D%0A        for (int i = 0; i %3C numQueries; i++) %7B%0D%0A            String str = sc.nextLine();%0D%0A            if (map.containsKey(str)) %7B%0D%0A                System.out.println(map.get(str));%0D%0A            %7D else %7B%0D%0A                System.out.println(%220%22);%0D%0A            %7D%0D%0A        %7D%0D%0A        sc.close();%0D%0A    %7D%0D%0A%7D

Add SparseArrays