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.
public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
// Write your code here
// Use a HashMap to store the frequency of each string in stringList
Map<String, Integer> frequencyMap = new HashMap<>();
// Count occurrences of each string in stringList
for (String str : stringList) {
frequencyMap.put(str, frequencyMap.getOrDefault(str, 0) + 1);
}
// Prepare the result list
List<Integer> result = new ArrayList<>();
// Retrieve frequency for each query
for (String query : queries) {
result.add(frequencyMap.getOrDefault(query, 0));
}
return result;
}
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 →