Given an array of strings arr[] . The task is to find the count of unordered pairs of strings (arr[i], arr[j]) , which in concatenation includes each character of the string “string” at least once.
Examples:
Input: arr[] = { “s”, “ng”, “stri”} Output: 1 (arr[1], arr[2]) is the only pair which on concatenation will contain every character of the string “string” i.e. arr[1] + arr[2] = “ngstri”
Input: arr[] = { “stri”, “ing”, “string” } Output: 3
Approach: Store the given strings as bit masks i.e. a string “srin” will be stored as 101110 as ‘t’ and ‘g’ are missing, so their corresponding bit will be 0 . Now, create an array of size 64 which is the maximum possible value of the bitmasks obtained (0 (000000) to 63 (111111)) . Now, the problem is reduced to finding the count of pairs of these bitmasks that give 63 (111111 in binary) as their OR value. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 64
int getBitmask(string s)
{
int temp = 0;
for (int j = 0; j < s.length(); j++) {
if (s[j] == 's') {
temp = temp | (1);
}
else if (s[j] == 't') {
temp = temp | (2);
}
else if (s[j] == 'r') {
temp = temp | (4);
}
else if (s[j] == 'i') {
temp = temp | (8);
}
else if (s[j] == 'n') {
temp = temp | (16);
}
else if (s[j] == 'g') {
temp = temp | (32);
}
}
return temp;
}
int countPairs(string arr[], int n)
{
int bitMask[MAX] = { 0 };
for (int i = 0; i < n; i++)
bitMask[getBitmask(arr[i])]++;
int cnt = 0;
for (int i = 0; i < MAX; i++) {
for (int j = i; j < MAX; j++) {
if ((i | j) == (MAX - 1)) {
if (i == j)
cnt += ((bitMask[i] * bitMask[i] - 1) / 2);
else
cnt += (bitMask[i] * bitMask[j]);
}
}
}
return cnt;
}
int main()
{
string arr[] = { "strrr", "string", "gstrin" };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countPairs(arr, n);
return 0;
}
Java
class GFG
{
static int MAX = 64;
static int getBitmask(char[] s)
{
int temp = 0;
for (int j = 0; j < s.length; j++)
{
switch (s[j])
{
case 's':
temp = temp | (1);
break;
case 't':
temp = temp | (2);
break;
case 'r':
temp = temp | (4);
break;
case 'i':
temp = temp | (8);
break;
case 'n':
temp = temp | (16);
break;
case 'g':
temp = temp | (32);
break;
default:
break;
}
}
return temp;
}
static int countPairs(String arr[], int n)
{
int []bitMask = new int[MAX];
for (int i = 0; i < n; i++)
bitMask[getBitmask(arr[i].toCharArray())]++;
int cnt = 0;
for (int i = 0; i < MAX; i++)
{
for (int j = i; j < MAX; j++)
{
if ((i | j) == (MAX - 1))
{
if (i == j)
cnt += ((bitMask[i] * bitMask[i] - 1) / 2);
else
cnt += (bitMask[i] * bitMask[j]);
}
}
}
return cnt;
}
public static void main(String[] args)
{
String arr[] = { "strrr", "string", "gstrin" };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
Python3
MAX = 64
def getBitmask(s):
temp = 0
for j in range(len(s)):
if (s[j] == 's'):
temp = temp | 1
elif (s[j] == 't'):
temp = temp | 2
elif (s[j] == 'r'):
temp = temp | 4
elif (s[j] == 'i'):
temp = temp | 8
elif (s[j] == 'n'):
temp = temp | 16
elif (s[j] == 'g'):
temp = temp | 32
return temp
def countPairs(arr, n):
bitMask = [0 for i in range(MAX)]
for i in range(n):
bitMask[getBitmask(arr[i])] += 1
cnt = 0
for i in range(MAX):
for j in range(i, MAX):
if ((i | j) == (MAX - 1)):
if (i == j):
cnt += ((bitMask[i] *
bitMask[i] - 1) // 2)
else:
cnt += (bitMask[i] * bitMask[j])
return cnt
arr = ["strrr", "string", "gstrin"]
n = len(arr)
print(countPairs(arr, n))
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 64;
static int getBitmask(char[] s)
{
int temp = 0;
for (int j = 0; j < s.Length; j++)
{
switch (s[j])
{
case 's':
temp = temp | (1);
break;
case 't':
temp = temp | (2);
break;
case 'r':
temp = temp | (4);
break;
case 'i':
temp = temp | (8);
break;
case 'n':
temp = temp | (16);
break;
case 'g':
temp = temp | (32);
break;
default:
break;
}
}
return temp;
}
static int countPairs(String []arr, int n)
{
int []bitMask = new int[MAX];
for (int i = 0; i < n; i++)
bitMask[getBitmask(arr[i].ToCharArray())]++;
int cnt = 0;
for (int i = 0; i < MAX; i++)
{
for (int j = i; j < MAX; j++)
{
if ((i | j) == (MAX - 1))
{
if (i == j)
cnt += ((bitMask[i] * bitMask[i] - 1) / 2);
else
cnt += (bitMask[i] * bitMask[j]);
}
}
}
return cnt;
}
public static void Main(String[] args)
{
String []arr = { "strrr", "string", "gstrin" };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
PHP
<?php
$MAX = 64;
function getBitmask($s)
{
$temp = 0;
for ($j = 0; $j < strlen($s); $j++)
{
if ($s[$j] == 's')
{
$temp = $temp | (1);
}
else if ($s[$j] == 't')
{
$temp = $temp | (2);
}
else if ($s[$j] == 'r')
{
$temp = $temp | (4);
}
else if ($s[$j] == 'i')
{
$temp = $temp | (8);
}
else if ($s[$j] == 'n')
{
$temp = $temp | (16);
}
else if ($s[$j] == 'g')
{
$temp = $temp | (32);
}
}
return $temp;
}
function countPairs($arr, $n)
{
$bitMask = array_fill(0, $GLOBALS['MAX'], 0);
for ($i = 0; $i < $n; $i++)
$bitMask[getBitmask($arr[$i])]++;
$cnt = 0;
for ($i = 0; $i < $GLOBALS['MAX']; $i++)
{
for ($j = $i; $j < $GLOBALS['MAX']; $j++)
{
if (($i | $j) == ($GLOBALS['MAX'] - 1))
{
if ($i == $j)
$cnt += floor(($bitMask[$i] *
$bitMask[$i] - 1) / 2);
else
$cnt += ($bitMask[$i] * $bitMask[$j]);
}
}
}
return $cnt;
}
$arr = array( "strrr", "string", "gstrin" );
$n = count($arr);
echo countPairs($arr, $n);
?>
Javascript
<script>
const MAX = 64;
function getBitmask(s) {
var temp = 0;
for (var j = 0; j < s.length; j++) {
switch (s[j]) {
case "s":
temp = temp | 1;
break;
case "t":
temp = temp | 2;
break;
case "r":
temp = temp | 4;
break;
case "i":
temp = temp | 8;
break;
case "n":
temp = temp | 16;
break;
case "g":
temp = temp | 32;
break;
default:
break;
}
}
return temp;
}
function countPairs(arr, n) {
var bitMask = new Array(MAX).fill(0);
for (var i = 0; i < n; i++)
bitMask[getBitmask(arr[i].split(""))]++;
var cnt = 0;
for (var i = 0; i < MAX; i++) {
for (var j = i; j < MAX; j++) {
if ((i | j) === MAX - 1) {
if (i === j)
cnt += parseInt((bitMask[i] * bitMask[i] - 1) / 2);
else
cnt += bitMask[i] * bitMask[j];
}
}
}
return cnt;
}
var arr = ["strrr", "string", "gstrin"];
var n = arr.length;
document.write(countPairs(arr, n));
</script>
Time Complexity: O(n * |s| + MAX2 )
Auxiliary Space: O(MAX)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule. Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...