Sort by

recency

|

2404 Discussions

|

  • + 0 comments
    public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
    // Write your code here
    // Use a HashMap to store the frequency of each string in stringList
        Map<String, Integer> frequencyMap = new HashMap<>();
    
        // Count occurrences of each string in stringList
        for (String str : stringList) {
            frequencyMap.put(str, frequencyMap.getOrDefault(str, 0) + 1);
        }
    
        // Prepare the result list
        List<Integer> result = new ArrayList<>();
    
        // Retrieve frequency for each query
        for (String query : queries) {
            result.add(frequencyMap.getOrDefault(query, 0));
        }
    
        return result;
    }
    
  • + 0 comments
    public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
        List<Integer> results = new ArrayList<>(); 
        for (String query: queries) {
            results.add(Collections.frequency(stringList, query));
        }
        return results; 
    }
    

    }

  • + 0 comments

    Perl:

    sub matchingStrings {
        my ($strings, $queries) = @_;
        
        my %h;
        my @res;
        foreach (@$strings) {
            $h{$_} +=1;
        }
        foreach my $el (@$queries) {
            if ($h{$el}) {
                push(@res, $h{$el});
            } else {
                push(@res, 0);
            }
            
        }    
        return @res;
    }
    
  • + 0 comments

    vector matchingStrings(vector stringList, vector queries) {

    unordered_map mymap; vector ans;

    for( auto i : stringList){ mymap[i]++; }

    for( auto i : queries){ ans.push_back(mymap[i]); }

    return ans; }

  • + 0 comments

    This task is a great exercise in working with collections and efficiently handling data queries. matchbox 9 registration