Sparse Arrays

Sort by

recency

|

232 Discussions

|

  • + 0 comments
    public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
        // Write your code here
        List<Integer> result = new ArrayList<Integer>();
        int size = strings.size();
        int length = queries.size();
        for(int i=0;i<length;i++){
            int count = 0;
            for(int j=0;j<size;j++){
                if(queries.get(i).equals(strings.get(j))){
                    count++;
                }
            }
            result.add(count);
        }
        return result;
    
        }
    
  • + 0 comments

    A simple solution for C++: vector matchingStrings(vector strings, vector queries) { unordered_map strMp; vector ans; for (string s: strings) strMp[s]++; for (string s: queries) ans.push_back(strMp[s]); return ans;

    }

  • + 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);
    }