Sort by

recency

|

2413 Discussions

|

  • + 0 comments

    Java Solution

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
        /*
         * Complete the 'matchingStrings' function below.
         *
         * The function is expected to return an INTEGER_ARRAY.
         * The function accepts following parameters:
         *  1. STRING_ARRAY stringList
         *  2. STRING_ARRAY queries
         */
    
        public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
        // Write your code here
        Integer[] result = new Integer[queries.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = 0;
        }
    
        stringList.sort(Comparator.comparingInt(String::length));
        
        for (int i = 0; i < queries.size(); i++) {
            
            for (String item : stringList){
                if(item.length() > queries.get(i).length()) break;
    
                if(item.equals(queries.get(i))){
                    result[i] += 1;
                }
            }
            
            
        }
        return Arrays.asList(result);
    
    
        }
    
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int stringListCount = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<String> stringList = IntStream.range(0, stringListCount).mapToObj(i -> {
                try {
                    return bufferedReader.readLine();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            })
                .collect(toList());
    
            int queriesCount = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<String> queries = IntStream.range(0, queriesCount).mapToObj(i -> {
                try {
                    return bufferedReader.readLine();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            })
                .collect(toList());
    
            List<Integer> res = Result.matchingStrings(stringList, queries);
    
            bufferedWriter.write(
                res.stream()
                    .map(Object::toString)
                    .collect(joining("\n"))
                + "\n"
            );
    
            bufferedReader.close();
            bufferedWriter.close();
        }
    }
    
  • + 0 comments

    Python

    def matchingStrings(stringList, queries):
        # Write your code here
        results = []
        for i in queries:
            counter = 0
            for j in stringList:
                if i == j:
                    counter += 1
            results.append(counter)
        return results
    
  • + 0 comments

    python solution

    def matchingStrings(stringList, queries):
        # Write your code here
    
        sortedList = sorted(stringList)
        ans = []
        idx = -1
        matchCount = 0
        for q in queries:
            idx = binary_search_bisect(sortedList, q)
    
            if idx == -1:
                ans.append(matchCount)
                continue
    
            for i in range(idx, len(sortedList)):
                if sortedList[i] != q:
                    break
                else:
                    matchCount = matchCount + 1
    
            ans.append(matchCount)
            matchCount = 0
    
        return ans
    
    def binary_search_bisect(sorted_list, target):
        i = bisect.bisect_left(sorted_list, target)
        if i != len(sorted_list) and sorted_list[i] == target:
            return i
        return -1
    
  • + 0 comments

    PHP

    function matchingStrings($stringList, $queries) {
        // Write your code here
        $counts = [];
        foreach ($queries as $i => $q)
        {
            $counts[$i] = 0;
            foreach ($stringList as $sl) {
                if ($sl == $q) {
                    $counts[$i]++;
                }
            }
        }
        return $counts;
    }
    
  • + 0 comments

    C# Linq Solution

    public static List<int> matchingStrings(List<string> stringList, List<string> queries)
    {
        List<int> results = new List<int>();
    
        foreach(string query in queries)
        {
            results.Add(
                stringList.Contains(query) ? stringList.Count(x => x == query) : 0
            );
        }
    
        return results;
    }