Pairs of strings which on concatenating contains each character of “string”


Introduction

A stream of characters flowing together collectively comprising of alphanumeric characters as well as special symbols can be termed as a C++ string.

String concatenation is an important aspect of the string data structure, since it is used to combine different sub strings or words to form complete sentences. Concatenation in C++ can be simply done using the + operator. In this article, we are going to develop a code that takes as input an array of string, and then analyses each pair to see if that pair contains all the letters of the word “string” . Let us look at the following example to understand the topic better −

Sample Example

Example 1 −

str − {"rtsr", "string", "gin","strin"}
Output − 5

In the following example, there are total 5 pairs containing all the letters of the word string −

{rtsr , string} , {rtsr , gin } , {string , strin } , {string , gin } , {gin , strin }

In this article, we will develop a code to process the pairs in the input array in the form of bit masks that correspond to characters of the word “string”. Since there are total 6 characters in the word “string”, the total number of combinations are equivalent to 2^6-1, which is equivalent to 63.

Syntax

str.length()

length()

Every C++ string is composed of a fixed number of alphanumeric characters. The length() method in C++ is used to compute the number of characters in the string.

sizeof(obj)

sizeof()

The sizeof() method is used to return the number of bytes allocated to the object invoking this method. The size if returned in the form of the multiple of the character variable size, owing to 1 Byte.

Algorithm

  • An input array of strings, arr is accepted.

  • The length of the string is computed using the length() method and stored in len variable.

  • The length of the word “string” is 6, therefore, the boolean combinations from 0-63 can be generated.

  • Every string corresponding to this array,arr is stored in the form of its bitmask.

  • The MAX value is stored to keep the total count of the bitmasks.

  • If a character in the accessed string of the arr, is contained in “string” , it is mapped to a value of 1 else 0. For instance, “srng” is mapped to 101011.,

  • After computing the bit mask of each of the strings, each pair is analysed independently.

  • maski corresponds to the mask of the ith string and maskj corresponds to the mask of the jth string. In case , the bit masks of both the strings combine to 63 (111111) , then it corresponds to the fact that all the letters of the word string are present in the pair.

  • In case i and j are equal, the count is incremented by (maski *maski -1)/2. Otherwise, count is incremented by (maski *maskj).

  • The count is then returned.

Example

The following C++ code snippet is used to take as input an array of strings and then evaluate all the pair counts containing all the characters of the word “string” −

#include <bits/stdc++.h>
using namespace std;
#define MAX 64
 
// Function to return the bitmask for the string
int masking(string str) {
   int len = str.length();
   int temp = 0;
   for (int j = 0; j < len; j++) {
      char ch = str[j];
      //if character equals s
      if (ch == 's') {
         temp = temp | (1);
      }
      //if equals t
      else if (ch == 't') {
         temp = temp | (2);
      }
      //if equal r
      else if (ch == 'r') {
          temp = temp | (4);
      }
      //if equals i 
      else if (ch == 'i') {
          temp = temp | (8);
      }
      else if (ch == 'n') {
          temp = temp | (16);
      }
      else if (ch == 'g') {
         temp = temp | (32);
      }
   } 
   return temp;
}
 
// Function to return the count of pairs
int pairswithString(string arr[], int n) {
   int cnt = 0;
   // bitMask[i] will store the count of strings whose bitmask is i
   int bit[64] = { 0 };
    
   for (int i = 0; i < n; i++)
      bit[masking(arr[i])]+=1;
 
   //looping through the maskings obtained
   for (int i = 0; i < MAX; i++) {
      for (int j = i; j < MAX; j++) {
         // MAX - 1 = 63 
         if ((i | j) == (MAX - 1)) {
            int maski = bit[i];
            int maskj = bit[j];
            //any element cannot be a pair with itself
            if (i == j)
               cnt += ((maski * maski - 1) / 2);
            else
               cnt += ( maski * maskj );
         }
      }
   }
   return cnt;
}
 
int main() {
   string arr[] = { "rtsr", "string", "gin","strin" };
   cout<<"Input array ";
   for(string s : arr)
      cout<<s<<"\n";
   int len = sizeof(arr) / sizeof(arr[0]);
   int count = pairswithString(arr, len);
   cout<< "Total count of pairs : "<<count<<"\n";
 
   return 0;
}

Output

Input array rtsr
string
gin
strin
Total count of pairs − 5

Conclusion

Bit masks can be easily used to manipulate and analyse strings. Bit masks can then be evaluated to see if their combinations match and then the total sum up til a number n can be evaluated by (n * n-1)/2;

Updated on: 31-Jul-2023

20 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements