Given two string S1 and S2, the task is to check whether both the strings can be made equal by performing the given operation on string S1. In a single operation, any character at an odd index can be swapped with any other character at an odd index, the same goes for the characters at even indices.
Input: S1 = “abcd”, S2 = “cbad”
Swap ‘a’ and ‘c’ in S1 and the resultant
string is equal to S2.
Input: S1 = “abcd”, S2 = “abcdcd”
- Create a string even_s1 from the characters at even indices from S1.
- Similarly, generate the strings even_s2, odd_s1 and odd_s2.
- Sort all the four strings from the previous steps.
- If even_s1 = even_s2 and odd_s1 = odd_s2 then print Yes.
- Else print No as the strings cannot be made equal.
Below is the implementation of the above approach:
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 whether two strings can be made equal by reversing substring of equal length from both strings
- 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 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
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if two strings can be made equal by swapping one character among each other
- Check if two arrays can be made equal by reversing subarrays multiple times
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Check if a two character string can be made using given words
- Check if one string can be converted to other using given operation
- Check whether given string can be generated after concatenating given strings
- Check if string can be made lexicographically smaller by reversing any substring
- Check whether second string can be formed from characters of first string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if all bits can be made same by flipping two consecutive bits
- Check if all strings of an array can be made same by interchanging characters
- Check if a given string is made up of two alternating characters
- Find the longest string that can be made up of other strings from the array
- Check whether the string can be printed using same row of qwerty keypad
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.