Created
May 25, 2022 19:25
-
-
Save Shaddyjr/ec559cbf1bc71a5bd4f359d79f343a39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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