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.
Examples:
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.
is_recursive_equiv(A, B):
if A == B:
return "YES" // base case: A and B are already equivalent
if length(A) is odd or length(B) is odd:
return "NO" // base case: odd-length strings cannot be recursively equivalent
n = length(A)
A1 = substring(A, 0, n/2) // divide A into two halves
A2 = substring(A, n/2)
B1 = substring(B, 0, n/2) // divide B into two halves
B2 = substring(B, n/2)
if (is_recursive_equiv(A1, B1) == "YES" and is_recursive_equiv(A2, B2) == "YES") or
(is_recursive_equiv(A1, B2) == "YES" and is_recursive_equiv(A2, B1) == "YES"):
return "YES" // if corresponding halves are recursively equivalent, return "YES"
return "NO" // otherwise, return "NO"
Implementation of above approach
C++
#include <iostream>
#include <string>
using namespace std;
string is_recursive_equiv(string A, string B) {
if (A == B) {
return "YES" ;
}
if (A.length() % 2 != 0 || B.length() % 2 != 0) {
return "NO" ;
}
int n = A.length();
string A1 = A.substr(0, n/2);
string A2 = A.substr(n/2);
string B1 = B.substr(0, n/2);
string B2 = B.substr(n/2);
if ((is_recursive_equiv(A1, B1) == "YES" && is_recursive_equiv(A2, B2) == "YES" ) ||
(is_recursive_equiv(A1, B2) == "YES" && is_recursive_equiv(A2, B1) == "YES" )) {
return "YES" ;
}
return "NO" ;
}
int main() {
string A = "aaba" ;
string B = "abaa" ;
cout << is_recursive_equiv(A, B) << endl;
A = "aabb" ;
B = "abab" ;
cout << is_recursive_equiv(A, B) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static String isRecursiveEquiv(String A, String B) {
if (A.equals(B)) {
return "YES" ;
}
if (A.length() % 2 != 0 || B.length() % 2 != 0 ) {
return "NO" ;
}
int n = A.length();
String A1 = A.substring( 0 , n / 2 );
String A2 = A.substring(n / 2 );
String B1 = B.substring( 0 , n / 2 );
String B2 = B.substring(n / 2 );
if ((isRecursiveEquiv(A1, B1).equals( "YES" ) && isRecursiveEquiv(A2, B2).equals( "YES" ))
|| (isRecursiveEquiv(A1, B2).equals( "YES" ) && isRecursiveEquiv(A2, B1).equals( "YES" ))) {
return "YES" ;
}
return "NO" ;
}
public static void main(String[] args) {
String A = "aaba" ;
String B = "abaa" ;
System.out.println(isRecursiveEquiv(A, B));
A = "aabb" ;
B = "abab" ;
System.out.println(isRecursiveEquiv(A, B));
}
}
|
Python
def is_recursive_equiv(A, B):
if A = = B:
return "YES"
if len (A) % 2 ! = 0 or len (B) % 2 ! = 0 :
return "NO"
n = len (A)
A1, A2 = A[:n / / 2 ], A[n / / 2 :]
B1, B2 = B[:n / / 2 ], B[n / / 2 :]
if (is_recursive_equiv(A1, B1) = = "YES" and is_recursive_equiv(A2, B2) = = "YES" ) or \
(is_recursive_equiv(A1, B2) = = "YES" and is_recursive_equiv(A2, B1) = = "YES" ):
return "YES"
return "NO"
A = "aaba"
B = "abaa"
print (is_recursive_equiv(A, B))
A = "aabb"
B = "abab"
print (is_recursive_equiv(A, B))
|
C#
using System;
class Program
{
static string IsRecursiveEquiv( string A, string B)
{
if (A == B)
{
return "YES" ;
}
if (A.Length % 2 != 0 || B.Length % 2 != 0)
{
return "NO" ;
}
int n = A.Length;
string A1 = A.Substring(0, n/2);
string A2 = A.Substring(n/2);
string B1 = B.Substring(0, n/2);
string B2 = B.Substring(n/2);
if ((IsRecursiveEquiv(A1, B1) == "YES" && IsRecursiveEquiv(A2, B2) == "YES" ) ||
(IsRecursiveEquiv(A1, B2) == "YES" && IsRecursiveEquiv(A2, B1) == "YES" ))
{
return "YES" ;
}
return "NO" ;
}
static void Main()
{
string A = "aaba" ;
string B = "abaa" ;
Console.WriteLine(IsRecursiveEquiv(A, B));
A = "aabb" ;
B = "abab" ;
Console.WriteLine(IsRecursiveEquiv(A, B));
}
}
|
Javascript
function isRecursiveEquiv(A, B) {
if (A === B) {
return "YES" ;
}
if (A.length % 2 !== 0 || B.length % 2 !== 0) {
return "NO" ;
}
const n = A.length;
const A1 = A.substring(0, n / 2);
const A2 = A.substring(n / 2);
const B1 = B.substring(0, n / 2);
const B2 = B.substring(n / 2);
if (
(isRecursiveEquiv(A1, B1) === "YES" && isRecursiveEquiv(A2, B2) === "YES" ) ||
(isRecursiveEquiv(A1, B2) === "YES" && isRecursiveEquiv(A2, B1) === "YES" )
) {
return "YES" ;
}
return "NO" ;
}
const A1 = "aaba" ;
const B1 = "abaa" ;
console.log(isRecursiveEquiv(A1, B1));
const A2 = "aabb" ;
const B2 = "abab" ;
console.log(isRecursiveEquiv(A2, B2));
|
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.
Steps to follow to implement the above approach:
- Define a function leastLexiString(s) that takes a string s as input and returns the least lexicographical string that can be formed by rearranging the letters of s.
- If the length of s is odd, return s as it is. This is the base case.
- Divide the string s into two halves, x and y.
- Recursively call leastLexiString() on x and y to find the least lexicographical strings that can be formed from these two halves.
- return min(x+y,y+x).
- Define a function areEquivalent(a, b) that takes two strings a and b as input and returns true if the least lexicographical strings that can be formed from a and b are equal, false otherwise.
- Call leastLexiString() on a and b, and compare the results. If they are equal, return true, otherwise return false.
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string leastLexiString(string s)
{
if (s.size() & 1)
return s;
string x = leastLexiString(s.substr(0,
s.size() / 2));
string y = leastLexiString(s.substr(s.size() / 2));
return min(x + y, y + x);
}
bool areEquivalent(string a, string b)
{
return (leastLexiString(a) == leastLexiString(b));
}
int main()
{
string a = "aaba" ;
string b = "abaa" ;
if (areEquivalent(a, b))
cout << "YES" << endl;
else
cout << "NO" << endl;
a = "aabb" ;
b = "abab" ;
if (areEquivalent(a, b))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
Java
class GfG
{
static String leastLexiString(String s)
{
if (s.length() == 1 )
return s;
String x = leastLexiString(s.substring( 0 ,
s.length() / 2 ));
String y = leastLexiString(s.substring(s.length() / 2 ));
return String.valueOf((x + y).compareTo(y + x));
}
static boolean areEquivalent(String a, String b)
{
return !(leastLexiString(a).equals(leastLexiString(b)));
}
public static void main(String[] args)
{
String a = "aaba" ;
String b = "abaa" ;
if (areEquivalent(a, b))
System.out.println( "Yes" );
else
System.out.println( "No" );
a = "aabb" ;
b = "abab" ;
if (areEquivalent(a, b))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def leastLexiString(s):
if ( len (s) & 1 ! = 0 ):
return s
x = leastLexiString(s[ 0 : int ( len (s) / 2 )])
y = leastLexiString(s[ int ( len (s) / 2 ): len (s)])
return min (x + y, y + x)
def areEquivalent(a,b):
return (leastLexiString(a) = = leastLexiString(b))
if __name__ = = '__main__' :
a = "aaba"
b = "abaa"
if (areEquivalent(a, b)):
print ( "YES" )
else :
print ( "NO" )
a = "aabb"
b = "abab"
if (areEquivalent(a, b)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static String leastLexiString(String s)
{
if (s.Length == 1)
return s;
String x = leastLexiString(s.Substring(0,
s.Length / 2));
String y = leastLexiString(s.Substring(
s.Length / 2));
return ((x + y).CompareTo(y + x).ToString());
}
static Boolean areEquivalent(String a, String b)
{
return !(leastLexiString(a).Equals(
leastLexiString(b)));
}
public static void Main(String[] args)
{
String a = "aaba" ;
String b = "abaa" ;
if (areEquivalent(a, b))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
a = "aabb" ;
b = "abab" ;
if (areEquivalent(a, b))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function leastLexiString(s)
{
if (s.length == 1)
return s;
let x = leastLexiString(s.substring(0,
s.length / 2));
let y = leastLexiString(s.substring(
s.length / 2));
if ((x + y) < (y + x))
{
return (x+y);
}
else {
return (y+x);
}
}
function areEquivalent(a, b)
{
return (leastLexiString(a) == leastLexiString(b));
}
let a = "aaba" ;
let b = "abaa" ;
if (areEquivalent(a, b))
document.write( "YES" + "</br>" );
else
document.write( "NO" + "</br>" );
a = "aabb" ;
b = "abab" ;
if (areEquivalent(a, b))
document.write( "YES" + "</br>" );
else
document.write( "NO" + "</br>" );
</script>
|
PHP
<?php
function leastLexiString( $s )
{
if ( strlen ( $s ) & 1)
return $s ;
$x = leastLexiString( substr ( $s , 0, floor ( strlen ( $s ) / 2)));
$y = leastLexiString( substr ( $s , floor ( strlen ( $s ) / 2), strlen ( $s )));
return min( $x . $y , $y . $x );
}
function areEquivalent( $a , $b )
{
return (leastLexiString( $a ) == leastLexiString( $b ));
}
$a = "aaba" ;
$b = "abaa" ;
if (areEquivalent( $a , $b ))
echo "YES" , "\n" ;
else
echo "NO" , "\n" ;
$a = "aabb" ;
$b = "abab" ;
if (areEquivalent( $a , $b ))
echo "YES" , "\n" ;
else
echo "NO" , "\n" ;
?>
|
Time Complexity: O(N*logN), where N is the size of the string.
Auxiliary Space: O(logN)
Contact Us