Given two strings str1 and str2, the task is to check whether both of the string can be made equal by copying any character of the string with its adjacent character. Note that this operation can be performed any number of times.
Input: str1 = “abc”, str2 = “def”
As all the characters in both the string are different.
So, there is no way they can be made equal.
Input: str1 = “abc”, str2 = “fac”
str1 = “abc” -> “aac”
str2 = “fac” -> “aac”
Approach: In order for the strings to be made equal with the given operation, they have to be of equal lengths and there has to be at least one character which is common in both the strings. To check that, create a frequency array freq which will store the frequency of all the characters of str1 and then for every character of str2 if its frequency in str1 is greater than 0 then it is possible to make both the strings 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
- Count of strings where adjacent characters are of difference one
- Check if a string is made up of K alternating characters
- Check if a given string is made up of two alternating characters
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Count the number of strings in an array whose distinct characters are less than equal to M
- Check whether two strings contain same characters in same order
- Program to check if first and the last characters of string are equal
- Print array of strings in sorted order without copying one string into another
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check if two strings after processing backspace character are equal or not
- String with k distinct characters and no same characters adjacent
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.