Sort by

recency

|

2389 Discussions

|

  • + 0 comments

    Creating a defaultdict makes this one pretty easy.

    ` from collections import defaultdict

    def matchingStrings(stringList, queries): hm = defaultdict(int) for key in stringList: hm[key] += 1 return [hm[key] for key in queries] `

  • + 0 comments

    javascript

      console.time("start");
      let result = [];
      for (let i = 0; i < queries.length; i++) {
        let sum = 0;
        for (let j = 0; j < stringList.length; j++) {
          if (stringList[j] === queries[i]) {
            sum += 1;
          }
        }
        result.push(sum);
      }
      console.timeEnd("start");
      return result;
    
  • + 0 comments

    c++ C++ cpp

    int main() { vector str = {"aba", "baba", "aba", "xzxb"}; vector query = {"aba", "xzxb", "ab"}; vector a; int counter=0;

    for (string qry : query) {
        //cout << qry << endl;
        for (string strng : str) {
            if(strng == qry){
                counter++;
            }
        }
        a.push_back(counter);
        counter=0;
    }
    
    // Display the updated vector
    for (int i : a) {
        cout << i << endl;
    }
    
    return 0;
    }
    
  • + 0 comments

    function matchingStrings(stringList, queries) { // Write your code here

    let results = []
    
    for(let i = 0; i < queries.length; i++ ){
    
    let found = stringList.filter((e) => e === queries[i])
    
       results[i] = found.length; 
    
    
    
    }
    
    return results
    

    }

  • + 0 comments

    go

    func matchingStrings(stringList []string, queries []string) []int32 {
    	// Write your code here
    	var ans = make([]int32, len(queries), len(queries))
    	if len(stringList) == 0 || len(queries) == 0 {
    		return ans
    	}
    	var sl = make(map[string]int32, len(stringList))
    	for _, s := range stringList {
    		if _, found := sl[s]; found {
    			sl[s]++
    		} else {
    			sl[s] = 1
    		}
    	}
    	for i, q := range queries {
    		if v, found := sl[q]; found {
    			ans[i] = v
    		}
    	}
    	fmt.Println(ans)
    	return ans
    }