Given two strings A and B of equal size. Two strings are equivalent either of the following conditions hold true:
1) They both are equal. Or,
2) If we divide the string A into two contiguous substrings of same size A1 and A2 and string B into two contiguous substrings of same size B1 and B2, then one of the following should be correct:
- A1 is recursively equivalent to B1 and A2 is recursively equivalent to B2
- A1 is recursively equivalent to B2 and A2 is recursively equivalent to B1
Check whether given strings are equivalent or not. Print YES or NO.
Input : A = “aaba”, B = “abaa”
Output : YES
Explanation : Since condition 1 doesn’t hold true, we can divide string A into “aaba” = “aa” + “ba” and string B into “abaa” = “ab” + “aa”. Here, 2nd subcondition holds true where A1 is equal to B2 and A2 is recursively equal to B1
Input : A = “aabb”, B = “abab”
Output : NO
Naive Solution : A simple solution is to consider all possible scenarios. Check first if the both strings are equal return “YES”, otherwise divide the strings and check if A1 = B1 and A2 = B2 or A1 = B2 and A2 = B1 by using four recursive calls. Complexity of this solution would be O(n2), where n is the size of the string.
Efficient Solution : Let’s define following operation on string S. We can divide it into two halves and if we want we can swap them. And also, we can recursively apply this operation to both of its halves. By careful observation, we can see that if after applying the operation on some string A, we can obtain B, then after applying the operation on B we can obtain A. And for the given two strings, we can recursively find the least lexicographically string that can be obtained from them. Those obtained strings if are equal, answer is YES, otherwise NO. For example, least lexicographically string for “aaba” is “aaab”. And least lexicographically string for “abaa” is also “aaab”. Hence both of these are equivalent.
Below is the implementation of the above approach.
Time Complexity : O(n), where n is the size of the string.
- Number of strings that satisfy the given condition
- Check if the string satisfies the given condition
- Meta Strings (Check if two strings can become same after a swap in one string)
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Program to check if two strings are same or not
- Check whether Strings are k distance apart or not
- Check if two strings are permutation of each other
- Check if two strings are k-anagrams or not
- Check if strings are rotations of each other or not | Set 2
- Check whether two strings are anagram of each other
- Check if two given strings are isomorphic to each other
- Check duplicates in a stream of strings
- Check whether two strings contain same characters in same order
- Check if there is any common character in two given strings
- Check if edit distance between two strings is one
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.