Problem Link - Count Queries on Strings
Problem Statement
You are given a list of N strings separated by spaces. After this, you are given K queries, where each query is a string q. For each query, your task is to determine how many times the query string q appears in the list.
Approach
To solve this problem, the idea is to first count the occurrences of each string in the list. This can be done using a dictionary (or hash map), where the keys are the strings, and the values are their counts. We loop through the given list of strings and populate the dictionary by incrementing the count for each string as it is encountered. Once this dictionary is prepared, it allows us to answer each query in constant time by simply looking up the query string in the dictionary.
Time Complexity
O(N + K) where N is the size of the list and K is the number of queries.
Space Complexity
O(U), where U is the number of unique strings in the list.