📜  从给定的字符串数组中计算相等的对

📅  最后修改于: 2021-10-27 07:08:21             🧑  作者: Mango

给定两个字符串数组s1[]s2[] 。任务是找到对(s1[i], s2[j])的计数,使得s1[i] = s2[j]请注意,元素s1[i]只能参与一对。

例子:

方法:

  • 创建一个 unordered_map 来存储数组s1[]的所有字符串的频率。
  • 现在对于所述阵列的每个字符串,检查一个字符串是否等于当前的字符串存在于地图或没有。
  • 如果是,则增加计数并减少映射中字符串的频率。这是因为一个字符串只能配对一次。
  • 最后打印计数

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of required pairs
int count_pairs(string s1[], string s2[], int n1, int n2)
{
 
    // Map to store the frequencies of
    // all the strings of array s1[]
    unordered_map mp;
 
    // Update the frequencies
    for (int i = 0; i < n1; i++)
        mp[s1[i]]++;
 
    // To store the count of pairs
    int cnt = 0;
 
    // For every string of array s2[]
    for (int i = 0; i < n2; i++) {
 
        // If current string can make a pair
        if (mp[s2[i]] > 0) {
 
            // Increment the count of pairs
            cnt++;
 
            // Decrement the frequency of the
            // string as once occurrence has been
            // used in the current pair
            mp[s2[i]]--;
        }
    }
 
    // Return the count
    return cnt;
}
 
// Driver code
int main()
{
    string s1[] = { "abc", "def" };
    string s2[] = { "abc", "abc" };
    int n1 = sizeof(s1) / sizeof(string);
    int n2 = sizeof(s2) / sizeof(string);
 
    cout << count_pairs(s1, s2, n1, n2);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
    // Function to return
    // the count of required pairs
    static int count_pairs(String s1[],
                           String s2[],
                           int n1, int n2)
    {
     
        // Map to store the frequencies of
        // all the strings of array s1[]
        HashMap mp = new HashMap();
 
        // Update the frequencies
        for (int i = 0; i < n1; i++)
            mp.put(s1[i], 0);
             
        // Update the frequencies
        for (int i = 0; i < n1; i++)
            mp.put(s1[i], mp.get(s1[i]) + 1);
     
        // To store the count of pairs
        int cnt = 0;
     
        // For every string of array s2[]
        for (int i = 0; i < n2; i++)
        {
     
            // If current string can make a pair
            if (mp.get(s2[i]) > 0)
            {
     
                // Increment the count of pairs
                cnt++;
     
                // Decrement the frequency of the
                // string as once occurrence has been
                // used in the current pair
                mp.put(s2[i], mp.get(s2[i]) - 1);
            }
        }
     
        // Return the count
        return cnt;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String s1[] = { "abc", "def" };
        String s2[] = { "abc", "abc" };
        int n1 = s1.length;
        int n2 = s2.length;
     
        System.out.println(count_pairs(s1, s2, n1, n2));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# python 3 implementation of the approach
 
# Function to return the count of required pairs
def count_pairs(s1, s2,n1,n2):
    # Map to store the frequencies of
    # all the strings of array s1[]
    mp = {s1[i]:0 for i in range(len(s1))}
 
    # Update the frequencies
    for i in range(n1):
        mp[s1[i]] += 1
 
    # To store the count of pairs
    cnt = 0
 
    # For every string of array s2[]
    for i in range(n2):
        # If current string can make a pair
        if (mp[s2[i]] > 0):
            # Increment the count of pairs
            cnt += 1
 
            # Decrement the frequency of the
            # string as once occurrence has been
            # used in the current pair
            mp[s2[i]] -= 1
 
    # Return the count
    return cnt
 
# Driver code
if __name__ == '__main__':
    s1 = ["abc", "def"]
    s2 = ["abc", "abc"]
    n1 = len(s1)
    n2 = len(s2)
 
    print(count_pairs(s1, s2, n1, n2))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // Function to return
    // the count of required pairs
    static int count_pairs(String []s1,
                           String []s2,
                           int n1, int n2)
    {
     
        // Map to store the frequencies of
        // all the strings of array s1[]
        Dictionary mp = new Dictionary();
 
        // Update the frequencies
        for (int i = 0; i < n1; i++)
            mp.Add(s1[i], 0);
             
        // Update the frequencies
        for (int i = 0; i < n1; i++)
        {
            var v = mp[s1[i]] + 1;
            mp.Remove(s1[i]);
            mp.Add(s1[i], v);
        }
     
        // To store the count of pairs
        int cnt = 0;
     
        // For every string of array s2[]
        for (int i = 0; i < n2; i++)
        {
     
            // If current string can make a pair
            if (mp[s2[i]] > 0)
            {
     
                // Increment the count of pairs
                cnt++;
     
                // Decrement the frequency of the
                // string as once occurrence has been
                // used in the current pair
                if(mp.ContainsKey(s2[i]))
                {
                    var v = mp[s2[i]] - 1;
                    mp.Remove(s2[i]);
                    mp.Add(s2[i], v);
                }
                else
                    mp.Add(s2[i], mp[s2[i]] - 1);
            }
        }
     
        // Return the count
        return cnt;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        String []s1 = { "abc", "def" };
        String []s2 = { "abc", "abc" };
        int n1 = s1.Length;
        int n2 = s2.Length;
     
        Console.WriteLine(count_pairs(s1, s2, n1, n2));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程