Count equal pairs from given string arrays
Last Updated :
03 Feb, 2023
Given two string arrays s1[] and s2[]. The task is to find the count of pairs (s1[i], s2[j]) such that s1[i] = s2[j]. Note that an element s1[i] can only participate in a single pair.
Examples:
Input: s1[] = {"abc", "def"}, s2[] = {"abc", "abc"}
Output: 1
Explanation: Only valid pair is (s1[0], s2[0]) or (s1[0], s2[1])
Note that even though "abc" appears twice in the
array s2[] but it can only make a single pair
as "abc" only appears once in the array s1[]
Input: s1[] = {"aaa", "aaa"}, s2[] = {"aaa", "aaa"}
Output: 2
Naive approach:
Generate all the pairs and check for the given condition, if it satisfy then increment the count by 1.
- Initialise a variable count = 0
- Generate all the pair
- Check if this pair is already considered, then continue
- Check if the pair satisfies the given condition
- Replace the string in s2 with "-1", just to mark that we have considered this string with some pair
- Increment the count by 1
- Return the count
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of required pairs
int count_pairs(string s1[], string s2[], int n1, int n2)
{
// Initialise a variable count = 0
int count = 0;
// Generate all the pair
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
// Check if this pair is already considered
// if true, then continue
if (s2[j] == "-1")
continue;
// Check if pair satisfy the given condition
if (s1[i] == s2[j]) {
// Replace the string in s2 with -1, just to
// mark that we have consider this string
// with some pair
s2[j] = "-1";
// Increment the count by 1
count++;
break;
}
}
}
// Return the count
return count;
}
// Driver code
int main()
{
string s1[] = { "abc", "def", "abc" };
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
import java.util.Arrays;
public class Gfg {
// Function to return the count of required pairs
public static int count_pairs(String s1[], String s2[],
int n1, int n2)
{
// Initialise a variable count = 0
int count = 0;
// Generate all the pair
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
// Check if this pair is already considered
// if true, then continue
if (s2[j] == "-1")
continue;
// Check if pair satisfy the given condition
if (s1[i].equals(s2[j])) {
// Replace the string in s2 with -1,
// just to mark that we have consider
// this string with some pair
s2[j] = "-1";
// Increment the count by 1
count++;
break;
}
}
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
String s1[] = { "abc", "def", "abc" };
String s2[] = { "abc", "abc" };
int n1 = s1.length;
int n2 = s2.length;
System.out.println(count_pairs(s1, s2, n1, n2));
}
}
Python3
# Python implementation of the approach
# Function to return the count of required pairs
def count_pairs( s1, s2, n1, n2):
# Initialise a variable count = 0
count = 0;
# Generate all the pair
for i in range(0,n1):
for j in range(0,n2):
# Check if this pair is already considered
# if true, then continue
if (s2[j] == "-1"):
continue;
# Check if pair satisfy the given condition
if (s1[i] == s2[j]) :
# Replace the string in s2 with -1, just to
# mark that we have consider this string
# with some pair
s2[j] = "-1";
# Increment the count by 1
count+=1;
break;
# Return the count
return count;
# Driver code
s1 = [ "abc", "def", "abc" ];
s2 = [ "abc", "abc" ];
n1 = len(s1);
n2 = len(s2);
print(count_pairs(s1, s2, n1, n2));
# This code is contributed by ratiagrawal.
C#
// C# code to implement the above idea
using System;
using System.Collections.Generic;
class GFG {
static int count_pairs(string[] s1, string[] s2, int n1, int n2)
{
// Initialise a variable count = 0
int count = 0;
// Generate all the pair
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
// Check if this pair is already considered
// if true, then continue
if (s2[j] == "-1")
continue;
// Check if pair satisfy the given condition
if (s1[i] == s2[j]) {
// Replace the string in s2 with -1, just to
// mark that we have consider this string
// with some pair
s2[j] = "-1";
// Increment the count by 1
count++;
break;
}
}
}
// Return the count
return count;
}
// Driver code
public static void Main()
{
string[] s1 = { "abc", "def", "abc" };
string[] s2 = { "abc", "abc" };
int n1 = s1.Length;
int n2 = s2.Length;
Console.Write(count_pairs(s1, s2, n1, n2));
}
}
// This code is contributed by poojaagarwal2.
JavaScript
// Javascript implementation of the approach
// Function to return the count of required pairs
function count_pairs(s1, s2, n1, n2)
{
// Initialise a variable count = 0
let count = 0;
// Generate all the pair
for (let i = 0; i < n1; i++) {
for (let j = 0; j < n2; j++) {
// Check if this pair is already considered
// if true, then continue
if (s2[j] == "-1")
continue;
// Check if pair satisfy the given condition
if (s1[i] == s2[j]) {
// Replace the string in s2 with -1, just to
// mark that we have consider this string
// with some pair
s2[j] = "-1";
// Increment the count by 1
count++;
break;
}
}
}
// Return the count
return count;
}
// Driver code
let s1 = [ "abc", "def", "abc" ];
let s2 = [ "abc", "abc" ];
let n1 = s1.length;
let n2 = s2.length;
document.write(count_pairs(s1, s2, n1, n2));
Time Complexity: O(n1*n2), Where n1 and n2 are the lengths of given string array s1 and s2 respectively.
Auxiliary Space: O(1)
Approach:
- Create an unordered_map to store the frequencies of all the strings of the array s1[].
- Now for every string of the array, check whether a string equal to the current string is present in the map or not.
- If yes then increment the count and decrement the frequency of the string from the map. This is because a string can only make a pair once.
- Print the count in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
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<string, int> 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<String,
Integer> mp = new HashMap<String,
Integer>();
// 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<String,
int> mp = new Dictionary<String,
int>();
// 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
<script>
// Javascript implementation of the approach
// Function to return the count of required pairs
function count_pairs(s1, s2, n1, n2)
{
// Map to store the frequencies of
// all the strings of array s1[]
var mp = new Map();
// Update the frequencies
for (var i = 0; i < n1; i++)
{
if(mp.has(s1[i]))
{
mp.set(s1[i], mp.get(s1[i])+1)
}
else
{
mp.set(s1[i], 1)
}
}
// To store the count of pairs
var cnt = 0;
// For every string of array s2[]
for (var 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.set(s2[i], mp.get(s2[i])-1)
}
}
// Return the count
return cnt;
}
// Driver code
var s1 = ["abc", "def" ];
var s2 = ["abc", "abc" ];
var n1 = s1.length;
var n2 = s2.length;
document.write( count_pairs(s1, s2, n1, n2));
</script>
Time Complexity: O(n1+n2), Where n1 and n2 are the lengths of given string array s1 and s2 respectively.
Auxiliary Space: O(n1)
Similar Reads
Count pairs from two arrays having sum equal to K Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with a sum equal to K. Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can parti
6 min read
Count unequal element pairs from the given Array Given an array arr[] of N elements. The task is to count the total number of indices (i, j) such that arr[i] != arr[j] and i < j.Examples: Input: arr[] = {1, 1, 2} Output: 2 (1, 2) and (1, 2) are the only valid pairs.Input: arr[] = {1, 2, 3} Output: 3Input: arr[] = {1, 1, 1} Output: 0 Recommended
10 min read
Count number of equal pairs in a string Given a string s, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different. Examples : Input: airOutput: 3Explanation :3 pairs that are equal are (a, a), (i, i) and (r, r)Input : geeksforgeeksOutput : 31Recommen
8 min read
Count pairs from two sorted arrays whose sum is equal to a given value x Given two sorted arrays of size m and n of distinct elements. Given a value x. The problem is to count all pairs from both arrays whose sum is equal to x. Note: The pair has an element from each array.Examples : Input : arr1[] = {1, 3, 5, 7} arr2[] = {2, 3, 5, 8} x = 10 Output : 2 The pairs are: (5,
15+ min read
Pairs from an array that satisfy the given condition Given an array arr[], the task is to count all the valid pairs from the array. A pair (arr[i], arr[j]) is said to be valid if func( arr[i] ) + func( arr[j] ) = func( XOR(arr[i], arr[j]) ) where func(x) returns the number of set bits in x. Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 3 (2, 4), (2
9 min read