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.
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 the binary equivalent of a number ends with given string or not
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Check if given Binary string follows then given condition or not
- Check whether Strings are k distance apart or not
- Check whether given string can be generated after concatenating given strings
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings contain same characters in same order
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check whether two strings are anagram of each other
- Convert given Float value to equivalent Fraction
- Print given sentence into its equivalent ASCII form
- Check whether the two Binary Search Trees are Identical or Not
- Number of strings that satisfy the given condition
- Check whether a given string is Heterogram or not
- Check whether a given number is an ugly number or not
- Convert the ASCII value sentence to its equivalent string
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
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.