Sparse Arrays

Sort by

recency

|

246 Discussions

|

  • + 0 comments

    Average-case time complexity: Building the frequency map: O(n) Answering queries with hash lookups: O(q)

    So the overall: O (n+q) on average

    vector<int> matchingStrings(vector<string> strings, vector<string> queries) {
        // hash map where the key is the string and the value is the frequency
        std::unordered_map<string, int> frequency_map;
        std::vector<int> result;
        // populate the frequency map
        for (const auto &word : strings){
            // the [] operator is asking:
            // If word exists in the map -> return a reference to its value 
            // If word does not exist -> insert it with a default value of 0, then return a reference to that 0
            ++frequency_map[word];
            // ++ on a hash map tells the code to increment
            // the value associated with the key! 
        }
        for (const auto &s : queries){
            std::cout << "Target string: " << s << std::endl;
            std::cout << "Frequency: " << frequency_map[s] << std::endl;
            result.push_back(frequency_map[s]);
        }
        return result;
    
    }
    
  • + 0 comments
    public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
    // Write your code here
        List<Integer> finalList = new ArrayList<>();
        Integer tempValue=0;
        for(String s1 :queries){
            for(String s2 :strings){
                if((s1.equals(s2))){
                    tempValue ++;
                }
            }
            finalList.add(tempValue);
            tempValue = 0;
        }
        return finalList;
    }
    
  • + 0 comments

    Java using a map to save the frequencies. This solution iterates each list only once.

        public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
        // Write your code here
            HashMap<String, Integer> stringsMap = new HashMap<>();
            for(String string : strings){
                if(stringsMap.containsKey(string)){
                    stringsMap.put(string, stringsMap.get(string) + 1);
                }else{
                    stringsMap.put(string, 1);
                }
            }
            
            ArrayList<Integer> results = new ArrayList<>(queries.size());
            for(String query: queries){
                Integer frequency = stringsMap.get(query);
                results.add(frequency == null ? 0 : frequency);
            }
            return results;
        }
    
  • + 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: Python:

    def matchingStrings(strings, queries):
        strings_frequency = {}
        for string in strings:
            strings_frequency[string] = strings_frequency.get(string, 0) + 1
    
        return [strings_frequency.get(query, 0) for query in queries]
    
  • + 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);
    }