Sparse Arrays

  • + 0 comments

    Java solution using HashMap:

        public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
    
            Map<String, Integer> countMap = new HashMap<>();
    
            for(String query : queries){
                countMap.put(query, 0);
            }
    
            for(String str : strings){
                if(countMap.containsKey(str)){
                    Integer count = countMap.get(str);
                    countMap.put(str, count + 1);
                }
            }
    
            List<Integer> returnList = new ArrayList<>();
            for(String query : queries){
                returnList.add(countMap.get(query));
            }
    
            return returnList;
        }