Sparse Arrays

  • + 0 comments

    Hi! This was my attempt using Javascript. I think its o(n) time or o(n+q) technically. I could probably avoid storing them in arrays in the groupedStrings array in hindsight

    function matchingStrings(strings, queries) {
        // Write your code here
        
        const groupedStrings = {};
        
        strings.forEach((string) => {
            if (!groupedStrings[string]) {
                groupedStrings[string] = []
            }
            groupedStrings[string].push(string);
        })
        
        queries.forEach((query) => {
            if (groupedStrings[query] == undefined) { 
                console.log(0);
                return;
            }
            console.log(groupedStrings[query].length);
        })
        
    }