Given two strings str1 and str2 of size N consisting of only three characters A, B, 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”.
Input: str1 = “BCCABCBCA”, str2 = “CBACCBBAC”
Transform the strings using following these steps:
BCCABCBCA -> CBCABCBCA -> CBACBCBCA -> CBACCBBCA -> CBACCBBAC.
Input: str1 = “BAC”, str2 = “CAB”
Naive Approach: The idea is to generate all possible strings recursively from the 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:
- 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.
- 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”.
- 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”.
- If the above two conditions are not satisfied, then print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
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.
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if one string can be converted to another
- Check if one string can be converted to other using given operation
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Check if a String can be converted to Pangram in K changes
- Check whether str1 can be converted to str2 with the given operations
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Rearrange string such that no pair of adjacent characters are of the same type
- Check if a string can be formed from another string using given constraints
- Minimum string such that every adjacent character of given string is still adjacent
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Maximum length of balanced string after swapping and removal of characters
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Check if a string can be obtained by rotating another string d places
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Smallest number possible by swapping adjacent even odd pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.