Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created May 25, 2022 19:25
Show Gist options
  • Save Shaddyjr/ec559cbf1bc71a5bd4f359d79f343a39 to your computer and use it in GitHub Desktop.
Save Shaddyjr/ec559cbf1bc71a5bd4f359d79f343a39 to your computer and use it in GitHub Desktop.
# source: https://www.hackerrank.com/challenges/sparse-arrays/problem
# Glitched Failure video: https://youtu.be/n9w7Nuy-FmA
function matchingStrings(strings, queries) {
const memory = {};
// O(n), where n = number of string
for(const string of strings) memory[string] = (memory[string] || 0) + 1;
// O(m), where m = number of queries (the lookup is constant time, O(1))
return queries.map(query=> memory[query] || 0);
// Total Time Complexity = O(n) + O(m) => O(n + m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment