Sparse Arrays

  • + 0 comments

    Here is the typescript solution for the better time complexity and space complexity. The time complexity over here will be o(n + q). I have created a hash map for the faster retrival of the information: Typescript:

    function matchingStrings(strings: string[], queries: string[]): number[] {
      // Write your code here
      const stringsFreq: { [key: string]: number } = {};
      for (const s of strings) {
        stringsFreq[s] = (stringsFreq[s] || 0) + 1;
      }
      return queries.map(q => stringsFreq[q] || 0);
    }