Sparse Arrays

Sort by

recency

|

230 Discussions

|

  • + 0 comments

    Here my Python Solution:

    def matchingStrings(strings, queries): # Write your code here count_list = []

    for i in queries:
        count_list.append(strings.count(i))
    
    return count_list
    
  • + 0 comments

    My Python 3 solution:

    def matchingStrings(strings, queries):
        for i in range(len(queries)):
            yield sum([1 for e in strings if queries[i] == e]) 
    
  • + 0 comments

    My Solution for C++ :

    vector<int> matchingStrings(vector<string> strings, vector<string> queries) {
        vector<int> ret;
        unordered_map<string, int> m;
        for (const string &s : strings) {
            m[s]++;
        }
        for (const string &q : queries) {
            if (m[q])
                ret.push_back(m[q]);
            else
                ret.push_back(0);
        }
        return (ret);
    }
    
  • + 0 comments

    Python 3 Solution:

    def matchingStrings(strings, queries):
        res = []
        count = 0
        for i in range(len(queries)):
            for j in range(len(strings)):
                if strings[j] == queries[i]:
                    count += 1
            res.append(count)
            count = 0
        return res
    
  • + 0 comments

    my version of code using JavaScript

    function matchingStrings(strings, queries) {
        // Write your code here
        const strCount = {};
        strings.forEach(str => {
            if(strCount[str])
            {
                strCount[str] += 1;
            } else {
                strCount[str] = 1;
            }
        });
        
        const result = queries.map(querie => {
            if(strCount[querie])
            {
                return strCount[querie];
            } else {
                return 0;
            }
        });
    
    return result;
    

    }