Check if string A can be converted to string B by changing A[i] to A[i+1] or A[i]..A[i+K-1] to A[i]+1 each


We are given two strings and we have to check if it is possible to convert the first string to another by performing any number of times a particular given task. The tasks that can be performed on the given first string only and the tasks are:

  • Choose any index i such that i < length(A) -1 and swap ith character with the next character.

  • We are given an integer k and we can choose any consecutive k indexes of the first string only if they are the same and can update them to their next character (only if possible, not possible for character z). For example: can update a to b, c to d, y to z, etc.

We have to tell whether we can make both the given string equal or not by applying the above-given tasks any number of times.

Sample Examples

Input

string str1 = “abcbcacba”
string str2 = “xxxyyyzzz”
int k = 3

Output

Yes, it is possible to convert the first string to the second

Explanation: We have three characters in the first string a,b, and c. Also, all three characters have the same frequency. We can increase the frequency of the characters 'a' all together to 'z' as we have three z's in the second string and similarly b to x, and c to y.

Input

string str1 = “abcbcacba”
string str2 = “xxxzzzyyy”
int k = 2;

Output

No, it is not possible to convert the first string to the second

Explanation: We can group only two characters as value of k is 2 and our different characters are in the pairs of 3 makes it is not possible to change them.

Observation

We have to make both strings equal and we can only apply the above-given operations. The observations we can make here are:

We can swap the characters any number of times so the order of the characters does not matter and the only matter is the frequency of the characters.

We can increase the frequency of the characters and not decrease it, so if there are some characters with an ASCII value greater than the greatest character of the second string then it is not possible to make both the strings same.

Also, if the size of both the strings is not the same then obviously it is not possible to make both the strings the same.

Approach

We have seen the examples and observations of the problem, let us move to the steps of the implementation:

  • First, we will check for the string lengths and if the lengths of the string are not the same then it is impossible to make them equal, so return false.

  • We will create two arrays to store the frequencies of the characters of both strings.

  • Then we will use the for loop to traverse over the strings to get the frequency.

  • After that, we will traverse the frequency arrays with an extra variable to count the extra characters which are left.

  • We will use if-else conditions to check if it is possible to make the string the same as the current index on the basis of the current index frequencies.

Example

#include <bits/stdc++.h>
using namespace std; 

bool isPos(string str1, string str2, int k){
   int len = str1.size(); // getting length of the string 1 
   // if the length of both the strings is not the same then return false
   if(len != str2.size()){
      return false;
   }
   // getting the frequency of the characters
   int freq1[26] = {0};
   int freq2[26] = {0}; 
   // getting the frequencies 
   for(int i=0; i<len; i++){
      freq1[str1[i]-'a']++;
   }
   for(int i=0; i<len; i++){
      freq2[str2[i]-'a']++;
   }
   int cnt = 0; // maintaining count of the remaining characters
   // traversing over the frequency array 
   for(int i=0; i<26; i++){
      freq1[i] += cnt; // adding the previous characters which are more 
      if(freq2[i] > freq1[i]){
         // we are not able to create the string 
         return false;
      }
      else if((freq1[i] - freq2[i]) % k){
         // some characters lefts which are not multiple of k and we are not able to make them same of another 
         return false;
      } else{
         cnt += freq1[i]-freq2[i];
      }
   }
   return true;
}
int main(){
   // given string 
   string str1 = "abccbabca";
   string str2 = "xxxzzzyyy";
   int k = 3; // given number 
   // calling to the function 
   bool res = isPos(str1, str2, k);
   if(res == true){
      cout<<"Yes, it is possible to convert the first string to the second"<<endl;
   } else{
      cout<<"No, it is not possible to convert the first string to the second"<<endl;
   }
}

Output

Yes, it is possible to convert the first string to the second

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given string.

The space complexity of the above code is O(1) or constant, as we use using some extra space but, that space is constant for all the input sizes.

Conclusion

In this tutorial, we have implemented a program to check if two given strings can be equal if we perform an unlimited number of given operations on the first string. The operations are: Choose any index i such that i < length(A) -1 and swap ith character with the next character, and we are given an integer k and we can choose any consecutive k indexes of the first string only if they are the same and can update them to their next character. We have implemented the program in linear time and constant space complexity.

Updated on: 31-Aug-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements