Given two strings str1 and str2, each of length N and consisting of lowercase English alphabets only, the task is to check if string str1 can be transformed to string str2 by performing the following operations any number of times.
- Choose a non-empty substring in str1 and sort it in-place lexicographically, to arrange the characters of the substring in non-decreasing order.
Input: str1 = “hdecb”, str2 = “cdheb”
Sorting the substring “ec” in str1 modifies the string to “hdceb”
Sorting the substring “hdc” in str1 modifies the string to “cdheb”.
Since, the modifed string is same as str2, the answer is Yes.
Input: str1 = “abcdef”, str2 = “dacbfe”
Approach: Follow the steps below to solve the problem:
- Observe that, in the string str1, if there are two characters str1[i] and str2[j] such that str1[i] < str1[j], then these characters can be swapped.
- In other words, it is possible to move a character freely to the left, until it encounters a smaller character. For example, “acdb” can be converted to either “acbd“, “abcd” as ‘b‘ can be moved freely to the left until ‘a‘ occurs.
- Therefore, check if it is possible to move the required characters to the left, to their respective positions in the string str2.
- Store the indices of every character of string str1 in an array.
- Traverse the string str2, and for each character, check if the same character in str1 can be shifted to that position or not.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
- Lexicographically smallest permutation of a string that contains all substrings of another string
- 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 converted to another string by replacing vowels and consonants
- 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
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting objects using In-Place sorting algorithm
- Check if a string can be split into even length palindromic substrings
- Check if a string can be split into substrings starting with N followed by N characters
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Check if one string can be converted to another
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if permutaion of one string can break permutation of another
- Sorting an array according to another array using pair in STL
- Count occurrences of a string that can be constructed from another given string
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.