We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticList<Integer>matchingStrings(List<String>strings,List<String>queries){// Write your code hereTreeMap<String,Integer>rMap=queries.stream().collect(Collectors.toMap(key->key,val->0,(n,p)->n,TreeMap::new));for(Stringstr:strings){if(rMap.containsKey(str)){rMap.put(str,rMap.get(str)+1);}}returnqueries.stream().map(q->rMap.get(q)).collect(Collectors.toList());}}
This has a runtime complexity of O(m + n) where m is the length of strings and n the length of queries.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
This is a possible solution in Java:
This has a runtime complexity of O(m + n) where m is the length of strings and n the length of queries.