Convert given Strings into T by replacing characters in between strings any number of times


Convert string means we have to make the same as a given string on the basis of a given condition. In this problem, we have given an array of strings ‘arr’ and string ‘T’ of size ‘M’. our task is to check if is it possible to make all the string present in an array as same as the given string T by removing any character from a string of an array ( arr[i] ) and inserting that character into any index of another string of the array ( arr[j] ). We can do this any number of times. If it is possible to make all the strings of the array the same as the string ‘T’ return “YES” otherwise return “NO”.

Sample Examples

Input 1: arr = [ “wxyz”, “wxxy”, “wyzz” ], T = “wxyz”
Output 1: YES

Explanation

One of the possible ways of making all the strings of the array as same as string T is as follows −

  • Remove the character of the string arr[1] (“wxxy”) at index 2 and insert it at the string arr[2] (“wyzz”) at index 1. Then it looks like: [ “wxyz”, “wxy”, “wxyzz”]

  • Remove the character of the string arr[2] (“wxyzz”) at index 3 and insert it at the string arr[1] (“wxy”) at index 3. Then it looks like: [ “wxyz”, “wxyz”, “wxyz”].

After performing the above steps we are able to make all the strings of the array is same as string T. Therefore the answer is “YES”.

Input 2: arr = [ “rts”, “rtw”, “rts” ], T = “rts”
Output 2: NO

Explanation

There are 3 strings present in the array out of which 2 are the same as string T but at a string of index number 1 is not the same. It contains a different character that is not part of the string T. It is impossible to make all the strings of the array make as string T. Therefore, the answer is “NO”.

Approach: Using Hashmap

We have seen the example above for the given string, let us move to the approach −

We have two observations as follows −

  • As we have to make all the strings of the array the same as string T so that all the characters of every string of the array must be present in the string T. In other words, no different character is present. Otherwise, we are not able to full fill condition.

  • When we are done with the count of the frequency of characters of all the strings of the array then the frequency count of each character must be equal to the size of the array ‘N’.

On the basis of the above observation, we have two conditions to check.

  • The hashmap of strings of array ‘freqArr’ size is equal to the hashmap ‘freqT’ of the string ‘T. As

freqArr.size() == freqT.size()
  • Each character of string T should be present in each string of the array. The frequency count of each character of string T in the array’s strings should be ‘N’. As-

freqArr.find(T[i]) == freqArr.end() and 
freqArr[T[i]] != freqT[T[i]]*N.

We can solve this problem using hashing as we need to count the frequency of the character of both the arrays string and string T.

Example

Let’s see the code of the above approach for a better understanding −

// Program to convert all strings to T
#include <bits/stdc++.h>
using namespace std;
string covertStringIntoT( int N, string arr[], string T){
   map< char,int > freqT; //to store the frequency of each character of string T
   int len = T.size(); //getting the size of the string T 
   
   //traverse the string T to store the frequency of the characters
   for( int i=0; i<len; i++){
      freqT[T[i]]++;
   }
   map< char,int > freqArr; //to store the frequency of each chracter of strings 
   
   // of Array.
   //traverse the strings of Array to store the frequency of the characters
   for( int i=0; i<N; i++){
      for(int j=0;j<arr[i].size(); j++){
         freqArr[arr[i][j]]++;
      }
   }
   
   // Check the condition one
   if(freqT.size() != freqArr.size()){
      return "NO";
   }    
   
   //check condition two while trversing the string T
   for( int i=0; i<len; i++){
      if(freqArr.find(T[i]) == freqArr.end() || freqArr[T[i]] != freqT[T[i]]*N ){
         return "NO";
      }
   }
   return "YES";
}
int main() {    
   string T = "wxyz"; // given string
   string arr[] = {"wxyz", "wxyy", "wxzz"}; // given array of strings
   int N = sizeof(arr) / sizeof(arr[0]); //getting the size of the array of string 
   
   // calling the function 'convertStringIntoT' to convert all strings of the 
   
   // array into string T
   string result = covertStringIntoT( N, arr, T);
   if(result == "YES"){
      cout<< result << ", it is possible to make all the strings of the array as string T";
   }
   else{
      cout<< result << ", it is not possible to make all the strings of the array as string T"; 
   }
   return 0;
}

Output

YES, it is possible to make all the strings of the array as string T

Time and Space Complexity

The time complexity of the above code is O(M + N*L)

The space complexity of the above code is O(M)

Where M is the size of the string T and N is the size of the Array and L is the longest string present in the array.

Conclusion

In this tutorial, we have implemented a program to Convert the given Strings into T by replacing characters in between strings any number of times. We have implemented an approach of hashing as we have to store the frequency. In this approach, we have to check mainly two conditions if all the conditions are satisfied means we are able to convert all the strings of the array same as string T.

Updated on: 25-Jul-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements