Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array


In this problem, we need to select the pair of strings and swap their first character. After that, we need to count the total number of new pairs. We can solve the problem by swapping the first character of each pair and checking whether it exists in the array.

The efficient approach to solve the problem can be using the hashmap data structure.

Problem statement – We have given an array containing N strings. We can take any of two strings from all array elements and swap the first characters of both strings. We need to count the total number of newly generated pairs of the string that are not present in the array.

Sample examples

Input – array[] = {"should", "would", "can"};

Output – 3

Explanation – The newly generated pairs can be could and wan. Another pair can be whould and sould. The third pair can be san and chould.

Input – array[] = {"demo", "test"};

Output – 1

Explanation – The newly generated pair which do not exist in the array is temo and dest.

Approach 1

In this approach, we will use two nested loops to get all pairs of the array elements. After that, we will swap the first characters of both pairs. Next, we will use a third nested loop to check whether the array contains the pair.

Algorithm

  • Define the ‘cnt’ variable and initialize with 0 to store a total number of pairs.

  • Use two nested loops to traverse the array of strings and get each pair of array elements.

  • Get both strings of the current pair.

  • If the first character of both strings is not equal, swap them

  • Define the ‘isFirst’ and ‘isSecond’ variables and initialize with false to track whether the newly generated string is present in the array.

  • Use the third nested loop to check whether a newly generated string is in the array. Also, update the value of the isFirst and isSecond variables based on the string in the array.

  • If both strings are not present in the array, increase the value of ‘cnt’ by 1.

  • Return the value of the ‘cnt’ variable.

Example

#include <iostream>
using namespace std;
// function to find the count of pairs of strings that can be formed by swapping the first character of the strings
int newStringPairs(string array[], int len){
   // Stores the count of pairs
   int cnt = 0;
   // Get all the pairs of strings
   for (int i = 0; i < len; i++){
      for (int j = i + 1; j < len; j++){
         // store single pair of strings
         string first = array[i], second = array[j];
         // If both strings' first characters are not equal, swap them.
         if (first[0] != second[0]){
            swap(first[0], second[0]);
            bool isFirst = false;
            bool isSecond = false;
            // Check whether the strings are present in the array or not
            for (int k = 0; k < len; k++){
               if (array[k] == first){
                  isFirst = true;
               }
                  if (array[k] == second){
                     isSecond = true;
                  }
              }
               // If both the strings are present in the array, then increment the cnt by 1
                if (isFirst == false && isSecond == false){
                    cnt++;
              }
          }
       }
    }
    return cnt;
}
int main(){
   string array[] = {"should", "would", "can"};
   int N = sizeof(array) / sizeof(array[0]);
   cout << "Total number of new pairs we can generate by swapping the first characters of given strings is " << newStringPairs(array, N);
   return 0;
}

Output

Total number of new pairs we can generate by swapping the first characters of given strings is 3

Time complexity – O(N^3), as we use three nested loops.

Space complexity – O(1)

Approach 2

In this approach, we will use the map data structure to store all array values in the map. After that, we can check whether the map contains a newly generated string. If not, we can increase the count value by 1.

Algorithm

  • Define the ‘cnt’ variable

  • Iterate through the array of strings, and store all array values in the map.

  • Use two nested loops to get all pairs of the array elements.

  • Get the string pairs and store them in the ‘first’ and ‘second’ variables.

  • If the first character of both strings is not equal, swap them.

  • In the map, check whether it contains a newly generated string. If not, increase the value of ‘cnt’ by 1.

  • Return the ‘cnt’ value.

Example

#include <iostream>
#include <unordered_map>
using namespace std;

// function to find the count of pairs of strings that can be formed by swapping the first character of the strings
int newStringPairs(string array[], int len){
    // to store the total number of new pairs
    int cnt = 0;
    // add all strings to the array map
    unordered_map<string, int> str_map;
    for (int i = 0; i < len; i++){
       str_map[array[i]]++;
    }
    //find all pairs of strings that can be formed by swapping the first character of the strings
    for (int i = 0; i < len; i++){
       for (int j = i + 1; j < len; j++){
          // get the current pair of strings
          string first = array[i];
          string second = array[j];
          // If the first character of both strings is not the same, then swap them
          if (first[0] != second[0]){
             swap(first[0], second[0]);
             // If both strings are not present in the map, then the increment count
             if (str_map[first] == 0 && str_map[second] == 0){
                cnt++;
               }
            }
         }
      }
   return cnt;
}
int main(){
   string array[] = {"should", "would", "can"};
   int N = sizeof(array) / sizeof(array[0]);
   cout << "Total number of new pairs we can generate by swapping the first characters of given strings is " << newStringPairs(array, N);
   return 0;
}

Output

Total number of new pairs we can generate by swapping the first characters of given strings is 3

Time complexity – O(N^2), as we use two nested loops.

Space complexity – O(N), as we use the map to store all array elements.

We learned the total number of newly generated pairs by swapping the first characters of any array elements. We have optimized the code in the second approach in the time complexity, but the first code is better in space complexity.

Updated on: 10-Aug-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements