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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if two strings can be made equal by swapping one character among each other
- 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 if a string is made up of K alternating characters
- Check if a given string is made up of two alternating characters
- Check if a two character string can be made using given words
- Check if string can be made lexicographically smaller by reversing any substring
- Program to check if first and the last characters of string are equal
- Check length of a string is equal to the number appended at its last
- Check if count of Alphabets and count of Numbers are equal in the given String
- 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
- Find the character made by adding all the characters of the given string
- 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 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.