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.
Example:
Input: S1 = “abbca”, S2 = “acabb”
Output: Yes
Explanation:
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”
Output: No
Approach:
- 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:
C++
// C++ program for the above approach#include <bits/stdc++.h> using namespace std; // function to count inversion // count of the string int inversionCount(string& s) { // for storing frequnecy int freq[26] = { 0 }; int inv = 0; for (int i = 0; i < s.length(); i++) { int temp = 0; // Add all the characters which // are less than the ith character // before i. for (int j = 0; j < int(s[i] - 'a'); j++) // adding the count to inversion // count temp += freq[j]; inv += (i - temp); // updating the character in // the frequency array freq[s[i] - 'a']++; } return inv; } // function to check whether any // of the string have a repeated // character bool haveRepeated(string& S1, string& S2) { int freq[26] = { 0 }; for (char i : S1) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } for (int i = 0; i < 26; i++) freq[i] = 0; for (char i : S2) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } return false; } // function to check whether the // string S1 and S2 can be made // equal by reversing sub strings // of same size in both strings void checkToMakeEqual(string S1, string S2) { // frequency array to check // whether both string have // same character or not int freq[26] = { 0 }; for (int i = 0; i < S1.length(); i++) { // adding the frequency; freq[S1[i] - 'a']++; } bool flag = 0; for (int i = 0; i < S2.length(); i++) { if (freq[S2[i] - 'a'] == 0) { // if the character is not in S1 flag = true; break; } // decrementing the frequency freq[S2[i] - 'a']--; } if (flag == true) { // If both string doesnot // have same characters or not cout << "No\n"; return; } // finding inversion count // of both strings int invCount1 = inversionCount(S1); int invCount2 = inversionCount(S2); if (invCount1 == invCount2 || (invCount1 & 1) == (invCount2 & 1) || haveRepeated(S1, S2)) { // If inversion count is same, // or have same parity or if // any of the string have a // repeated character then // the answer is Yes else No cout << "Yes\n"; } else cout << "No\n"; } // driver code int main() { string S1 = "abbca", S2 = "acabb"; checkToMakeEqual(S1, S2); return 0; } |
Python3
# Python3 program for the above approach# function to count inversion # count of the string def inversionCount(s): # for storing frequnecy freq = [0 for _ in range(26)] inv = 0 for i in range(len(s)): # we'll add all the characters # which are less than the ith # character before i. temp = 0 for j in range(ord(s[i]) - ord('a')): # adding the count to # inversion count temp += freq[j] inv += (i - temp) # updating the character in # the frequency array freq[ord(s[i]) - ord('a')] += 1 return inv # function to check whether # any of the string have a # repeated character def haveRepeated(S1, S2): freq = [0 for _ in range(26)] for i in range(len(S1)): if freq[ord(S1[i]) - ord('a')] > 0: return 1 freq[ord(S1[i]) - ord('a')] += 1 for i in range(26): freq[i] = 0 for i in range(len(S2)): if freq[ord(S2[i]) - ord('a')] > 0: return 1 freq[ord(S2[i]) - ord('a')] += 1 return 0# function to check whether # the string S1 and S2 can # be made equal by reversing # sub strings ofsame size in # both strings def checkToMakeEqual(S1, S2): # frequency array to check # whether both string have # same character or not freq = [0 for _ in range(26)] for i in range(len(S1)): # adding the frequency; freq[ord(S1[i]) - ord('a')] += 1 flag = 0 for i in range(len(S2)): if freq[ord(S2[i]) - ord('a')] == 0: # if the character is not in S1 flag = 1 break # decrementing the frequency freq[ord(S2[i]) - ord('a')] -= 1 if flag == 1: # If both string does not # have same characters or not print("No") return # finding inversion count of # both strings invCount1 = inversionCount(S1) invCount2 = inversionCount(S2) if ((invCount1 == invCount2) or ((invCount1 % 2) == (invCount2 % 2)) or haveRepeated(S1, S2) == 1): # If inversion count is same, # or have same parity # or if any of the string # have a repeated character # then the answer is Yes else No print("Yes") else: print("No") # Driver Code S1 = "abbca"S2 = "acabb"checkToMakeEqual(S1, S2) |
Java
// Java program for the above approachimport java.io.*;import java.util.*;class GFG{ // Function to count inversion// count of the stringstatic int inversionCount(String s){ // For storing frequnecy int[] freq = new int[26]; int inv = 0; for(int i = 0; i < s.length(); i++) { int temp = 0; // Add all the characters which // are less than the ith character // before i. for(int j = 0; j < (int)(s.charAt(i) - 'a'); j++) // Adding the count to inversion // count temp += freq[j]; inv += (i - temp); // Updating the character in // the frequency array freq[s.charAt(i) - 'a']++; } return inv;}// Function to check whether any// of the string have a repeated// characterstatic boolean haveRepeated(String S1, String S2){ int[] freq = new int[26]; for(char i : S1.toCharArray()) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } for(int i = 0; i < 26; i++) freq[i] = 0; for(char i : S2.toCharArray()) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } return false;}// Function to check whether the// string S1 and S2 can be made// equal by reversing sub strings// of same size in both stringsstatic void checkToMakeEqual(String S1, String S2){ // Frequency array to check // whether both string have // same character or not int[] freq = new int[26]; for(int i = 0; i < S1.length(); i++) { // Adding the frequency; freq[S1.charAt(i) - 'a']++; } boolean flag = false; for(int i = 0; i < S2.length(); i++) { if (freq[S2.charAt(i) - 'a'] == 0) { // If the character is not in S1 flag = true; break; } // Decrementing the frequency freq[S2.charAt(i) - 'a']--; } if (flag == true) { // If both string doesnot // have same characters or not System.out.println("No"); return; } // Finding inversion count // of both strings int invCount1 = inversionCount(S1); int invCount2 = inversionCount(S2); if (invCount1 == invCount2 || (invCount1 & 1) == (invCount2 & 1) || haveRepeated(S1, S2)) { // If inversion count is same, // or have same parity or if // any of the string have a // repeated character then // the answer is Yes else No System.out.println("Yes"); } else System.out.println("No");}// Driver Codepublic static void main (String[] args){ String S1 = "abbca", S2 = "acabb"; checkToMakeEqual(S1, S2);}}// This code is contributed by offbeat |
C#
// C# program for the above approachusing System;class GFG{ // Function to count inversion// count of the stringstatic int inversionCount(String s){ // For storing frequnecy int[] freq = new int[26]; int inv = 0; for(int i = 0; i < s.Length; i++) { int temp = 0; // Add all the characters which // are less than the ith character // before i. for(int j = 0; j < (int)(s[i] - 'a'); j++) // Adding the count to inversion // count temp += freq[j]; inv += (i - temp); // Updating the character in // the frequency array freq[s[i] - 'a']++; } return inv;}// Function to check whether any// of the string have a repeated// characterstatic bool haveRepeated(String S1, String S2){ int[] freq = new int[26]; foreach(char i in S1.ToCharArray()) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } for(int i = 0; i < 26; i++) freq[i] = 0; foreach(char i in S2.ToCharArray()) { if (freq[i - 'a'] > 0) return true; freq[i - 'a']++; } return false;}// Function to check whether the// string S1 and S2 can be made// equal by reversing sub strings// of same size in both stringsstatic void checkToMakeEqual(String S1, String S2){ // Frequency array to check // whether both string have // same character or not int[] freq = new int[26]; for(int i = 0; i < S1.Length; i++) { // Adding the frequency; freq[S1[i] - 'a']++; } bool flag = false; for(int i = 0; i < S2.Length; i++) { if (freq[S2[i] - 'a'] == 0) { // If the character is not in S1 flag = true; break; } // Decrementing the frequency freq[S2[i] - 'a']--; } if (flag == true) { // If both string doesnot // have same characters or not Console.WriteLine("No"); return; } // Finding inversion count // of both strings int invCount1 = inversionCount(S1); int invCount2 = inversionCount(S2); if (invCount1 == invCount2 || (invCount1 & 1) == (invCount2 & 1) || haveRepeated(S1, S2)) { // If inversion count is same, // or have same parity or if // any of the string have a // repeated character then // the answer is Yes else No Console.WriteLine("Yes"); } else Console.WriteLine("No");}// Driver Codepublic static void Main(String[] args){ String S1 = "abbca", S2 = "acabb"; checkToMakeEqual(S1, S2);}}// This code is contributed by gauravrajput1 |
Yes
Time Complexity:
![]()
Space Complexity:
![]()
Recommended Posts:
- 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 contribute@geeksforgeeks.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.


