Sort by

recency

|

2425 Discussions

|

  • + 0 comments

    JavaScript Solution:

    let res=[];
    let count=0;
    queries.map(s=>{
        count=0;
        stringList.map(q=>{
            if(s===q) count++;
        })
        res.push(count);
    })
    return res;
    
  • + 0 comments

    Here's my easy understandable solution in C++, just ask gpt to do formatting of code (if not presentable).

    include

    using namespace std; void matching_func(string str_list[], int str_size, string que_list[], int que_size) { // this function will match the queries and str indexes and // also directly print the results. int result; for(int q_index = 0; q_index < que_size; q_index++) { result = 0; // to make results 0 for each query start for(int str_index = 0; str_index < str_size; str_index++) {
    // this inner loop will match specific query index element to string index elements. if(que_list[q_index] == str_list[str_index]) { result++; // if matched, then only increase the value } } cout << result << endl; } } void input_arr(int size, string arr[]) { for (int index = 0; index < size; index++) { cin >> arr[index]; } } int main() { int size_string_list, size_query_list; cin >> size_string_list; string string_list[size_string_list]; input_arr(size_string_list, string_list); cin >> size_query_list; string query_list[size_query_list]; input_arr(size_query_list, query_list); matching_func(string_list, size_string_list, query_list, size_query_list); // here we have just taken the input of size and list return 0; }

  • + 0 comments

    The crux here is that the criteria does not mention uniqueness in the queries. That's bad and makes this problem bad since I can't ask the writer if that is a part of the criteria or not.

    You'd also not want to repeat queries if you don't have to in the real world.

  • + 0 comments

    Rust

    fn matching_strings_v2(string_list: &[String], queries: &[String]) -> Vec<i32> {
        let mut answer:Vec<i32> = vec![];
        let mut string_vec = string_list.to_vec();
        let mut map = HashMap::<&str, i32>::new();
        
        string_vec.sort_unstable();
        for q in queries {
            map.entry(q)
            .and_modify(|a| {
                answer.push(*a);
            })
            .or_insert_with(|| {
                if let Ok(idx) = string_vec.binary_search(q) {
                    let mut count = 1;
                    count += v2_count_occurence(&string_vec[..], &q, idx, true);
                    count += v2_count_occurence(&string_vec[..], &q, idx, false);
                    answer.push(count);
                    count
                } else {
                    answer.push(0);
                    0
                }
            });
        }
        answer
    }
    
    fn v2_count_occurence(arr: &[String] , find: &str, idx: usize, left_or_right: bool) -> i32 {
        let len = arr.len();
        let step = if left_or_right { -1i32 } else { 1 };
        let start = idx as i32 + step;
        let mut i = start;
        let mut count = 0;
        loop {
            let _i = i as usize;
            if i < 0 || _i >= len {
                break;
            }
            if arr[_i] != find {
                break;
            }
            count += 1;
            i += step;
        }
        count
    }
    
  • + 0 comments

    1. Time Complexity O(n + q)

    2. Space Complexity O(n + q)

    vector matchingStrings(const vector& strings, const vector& queries) {

     unorderedmap<string, int> freqMap;
    
     for (const string& str : strings) {
         freqMap[str]++;
     }
    
         vector<int> results;
    
     for (const string& query : queries) {
             results.pushback(freqMap[query]);
        }
    
    return results;
    

    }