Give two strings S1 and S2, the task is to check whether string S1 can be made equal to string S2 by reversing substring from both strings of equal length.
Note: A substring can be reversed any number of times.
Input: S1 = “abbca”, S2 = “acabb”
The string S1 and S2 can be made equal by:
Reverse S1 in the range [2, 4] (length = 3), S1 = “abacb”
Reverse S2 in the range [1, 3] (length = 3), S2 = “abacb”
S1 = “abacb” and S2 = “abacb”, after reversing.
Hence, both can be made equal.
Input: “S1 = “abcd”, S2 = “abdc”
- The idea is to make both the strings equal by sorting both of them.
- First, check if both the strings have the same set of characters or not. If not, then answer is “No”.
- Fix the length of substring to reverse to 2. Now, this means swapping adjacent characters.
- Now, number of moves needed to sort a string by reversing substring of size 2 or in other word, swapping adjacent characters, is the Inversion Count of the string.
- If both strings have the same inversion count, then the answer is “Yes”.
- If they have different inversion counts, making them equal is only possible if at least one of the following conditions matches:
- First, if the parity of the inversion count is the same, that is both even or odd, then answer is “Yes”. We’ll continue swapping any pair of elements in the sorted string until the other one is sorted. Since, the difference of inversion count will be even, so swapping any pair twice doesn’t produce any change.
- Second, if the parity is not the same, then there has to be at least a character with a frequency greater than 1 in any of the string. We’ll just swap them until the other one gets sorted. Since swapping identical character doesn’t make any change.
Below is the implementation of the above approach:
- Check if string can be made lexicographically smaller by reversing any substring
- Check if two arrays can be made equal by reversing subarrays multiple times
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if two strings can be made equal by swapping one character among each other
- Check whether an array can be made strictly decreasing by modifying at most one element
- Check whether an array can be made strictly increasing by modifying atmost one element
- Check whether it is possible to make both arrays equal by modifying a single element
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Check if all strings of an array can be made same by interchanging characters
- Check if a two character string can be made using given words
- Check if all bits can be made same by flipping two consecutive bits
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Maximum number of parallelograms that can be made using the given length of line segments
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Minimum length of substring whose rotation generates a palindromic substring
- Print all possible strings that can be made by placing spaces
- Find the longest string that can be made up of other strings from the array
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.