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.
Examples:
Input: str1 = “abc”, str2 = “def”
Output: No
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”
Output: Yes
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 26
bool canBeMadeEqual(string str1, string str2)
{
int len1 = str1.length();
int len2 = str2.length();
if (len1 == len2) {
int freq[MAX];
for ( int i = 0; i < len1; i++) {
freq[str1[i] - 'a' ]++;
}
for ( int i = 0; i < len2; i++) {
if (freq[str2[i] - 'a' ] > 0)
return true ;
}
}
return false ;
}
int main()
{
string str1 = "abc" , str2 = "defa" ;
if (canBeMadeEqual(str1, str2))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int MAX = 26 ;
static boolean canBeMadeEqual(String str1,
String str2)
{
int len1 = str1.length();
int len2 = str2.length();
if (len1 == len2)
{
int freq[] = new int [MAX];
for ( int i = 0 ; i < len1; i++)
{
freq[str1.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < len2; i++)
{
if (freq[str2.charAt(i) - 'a' ] > 0 )
return true ;
}
}
return false ;
}
public static void main (String[] args)
{
String str1 = "abc" , str2 = "defa" ;
if (canBeMadeEqual(str1, str2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
MAX = 26
def canBeMadeEqual(str1, str2):
len1 = len (str1)
len2 = len (str2)
if (len1 = = len2):
freq = [ 0 for i in range ( MAX )]
for i in range (len1):
freq[ ord (str1[i]) - ord ( 'a' )] + = 1
for i in range (len2):
if (freq[ ord (str2[i]) - ord ( 'a' )] > 0 ):
return True
return False
str1 = "abc"
str2 = "defa"
if (canBeMadeEqual(str1, str2)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int MAX = 26;
static Boolean canBeMadeEqual(String str1,
String str2)
{
int len1 = str1.Length;
int len2 = str2.Length;
if (len1 == len2)
{
int []freq = new int [MAX];
for ( int i = 0; i < len1; i++)
{
freq[str1[i] - 'a' ]++;
}
for ( int i = 0; i < len2; i++)
{
if (freq[str2[i] - 'a' ] > 0)
return true ;
}
}
return false ;
}
public static void Main (String []args)
{
String str1 = "abc" , str2 = "defa" ;
if (canBeMadeEqual(str1, str2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
let MAX = 26;
function canBeMadeEqual(str1, str2)
{
let len1 = str1.length;
let len2 = str2.length;
if (len1 == len2)
{
let freq = new Array(MAX);
freq.fill(0);
for (let i = 0; i < len1; i++)
{
freq[str1[i].charCodeAt() -
'a' .charCodeAt()]++;
}
for (let i = 0; i < len2; i++)
{
if (freq[str2[i].charCodeAt() -
'a' .charCodeAt()] > 0)
return true ;
}
}
return false ;
}
let str1 = "abc" , str2 = "defa" ;
if (canBeMadeEqual(str1, str2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(26)