Given two strings str1 and str2 of size N consisting of only three characters AB, and C, the task is to check whether the string str1 can be changed to str2 using the below operations:

  • Replacing one occurrence of “BC” with “CB” i.e swap adjacent ‘B’ and ‘C’.
  • Replacing one occurrence of “CA” with “AC” i.e swap adjacent ‘C’ and ‘A’.

Print “Yes” if we can transform the string else print “No”.

Examples:

Input:_ str1 = “BCCABCBCA”, str2 = “CBACCBBAC” _

Output:_ Yes _

Explanation:

_Transform the strings using following these steps: _

BCCABCBCA -> CBCABCBCA -> CBACBCBCA -> CBACCBBCA -> CBACCBBAC.

Input:_ str1 = “BAC”, str2 = “CAB” _

Output:_ False_

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The idea is to generate all possible strings recursively from start of the string str1 by performing the given operations and store it in a set of strings. Then check if any string from the set is equal to the string str2 or not. If string str2 is found in the set then print “Yes” else print “No”.

Time Complexity:_ O(2N) _

Auxiliary Space:_ O(1)_

Efficient Approach: The idea is to transverse the two strings simultaneously and check if it’s possible to transform the string str1 to str2 till a particular index. Below are the steps:

  1. Check for the sequence ‘A’ and ‘B’ in the strings str1 and str2, if it’s the same, then proceed to the second step. Otherwise, print “No” as the desired conversion is not possible.
  2. The index of ‘A’ in str1 should be greater than and equal to the index of the corresponding ‘A’ in str2, since “CA” can be converted only to “AC”.
  3. Similarly, the index of ‘B’ in str1 string should be less than or equal to the corresponding index of ‘B’ in str2, since “BC” can only be converted to “CB”.
  4. If the above two conditions are not satisfied, then print “No”. Otherwise, print “Yes”.

Below is the implementation of the above approach:

  • C++
  • Python3

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check if it is possible

// to transform start to end

**bool** canTransform(string str1,

string str2)

{

string s1 = ""``;

string s2 = ""``;

// Check the sequence of A, B in

// both strings str1 and str2

**for** (``**char** c : str1) {

**if** (c != 'C'``) {

s1 += c;

}

}

**for** (``**char** c : str2) {

**if** (c != 'C'``) {

s2 += c;

}

}

// If both the strings

// are not equal

**if** (s1 != s2)

**return** **false**``;

**int** i = 0;

**int** j = 0;

**int** n = str1.length();

// Traverse the strings

**while** (i < n and j < n) {

**if** (str1[i] == 'C'``) {

i++;

}

**else** **if** (str2[j] == 'C'``) {

j++;

}

// Check for indexes of A and B

**else** {

**if** ((str1[i] == 'A'

and i < j)

or (str1[i] == 'B'

and i > j)) {

**return** **false**``;

}

i++;

j++;

}

}

**return** **true**``;

}

// Driver Code

**int** main()

{

string str1 = "BCCABCBCA"``;

string str2 = "CBACCBBAC"``;

// Function Call

**if** (canTransform(str1, str2)) {

cout << "Yes"``;

}

**else** {

cout << "No"``;

}

**return** 0;

}

Output:

Yes

Time Complexity:_ O(N) _

Auxiliary Space:_ O(1) _

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#pattern searching #searching #strings

Check if a string can be converted to another by swapping of adjacent characters
24.95 GEEK