Check if a string can be made equal to another string by swapping or replacement of characters
Last Updated :
19 May, 2021
Given two strings S1 and S2, the task is to check if string S1 can be made equal to string S2 by swapping any pair of characters replacing any character in the string S1. If it is possible to make the string S1 equal to S2, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S1 = “abc”, S2 = “bca”
Output: Yes
Explanation:
Operation 1: “abc” -> “acb”
Operation 2: “acb” -> “bca”
Input: S1 = “a”, S2 = “aa”
Output: No
Explanation: It is impossible to make the two strings same.
Approach: The idea to solve this problem is to find the frequencies of the characters in the strings and check that the same character is being used in both strings or not. Follow the steps below to solve the problem:
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool sameStrings(string str1,
string str2)
{
int N = str1.length();
int M = str2.length();
if (N != M) {
return false ;
}
int a[256] = { 0 }, b[256] = { 0 };
for ( int i = 0; i < N; i++) {
a[str1[i] - 'a' ]++;
b[str2[i] - 'a' ]++;
}
int i = 0;
while (i < 256) {
if ((a[i] == 0 && b[i] == 0)
|| (a[i] != 0 && b[i] != 0)) {
i++;
}
else {
return false ;
}
}
sort(a, a + 256);
sort(b, b + 256);
for ( int i = 0; i < 256; i++) {
if (a[i] != b[i])
return false ;
}
return true ;
}
int main()
{
string S1 = "cabbba" , S2 = "abbccc" ;
if (sameStrings(S1, S2))
cout << "YES" << endl;
else
cout << " NO" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean sameStrings(String str1,
String str2)
{
int N = str1.length();
int M = str2.length();
if (N != M)
{
return false ;
}
int []a = new int [ 256 ];
int []b = new int [ 256 ];
for ( int i = 0 ; i < N; i++)
{
a[str1.charAt(i) - 'a' ]++;
b[str2.charAt(i) - 'a' ]++;
}
int i = 0 ;
while (i < 256 )
{
if ((a[i] == 0 && b[i] == 0 )
|| (a[i] != 0 && b[i] != 0 ))
{
i++;
}
else
{
return false ;
}
}
Arrays.sort(a);
Arrays.sort(b);
for (i = 0 ; i < 256 ; i++)
{
if (a[i] != b[i])
return false ;
}
return true ;
}
public static void main(String[] args)
{
String S1 = "cabbba" , S2 = "abbccc" ;
if (sameStrings(S1, S2))
System.out.print( "YES" + "\n" );
else
System.out.print( " NO" + "\n" );
}
}
|
Python3
def sameStrings(str1, str2):
N = len (str1)
M = len (str2)
if (N ! = M):
return False
a, b = [ 0 ] * 256 , [ 0 ] * 256
for i in range (N):
a[ ord (str1[i]) - ord ( 'a' )] + = 1
b[ ord (str2[i]) - ord ( 'a' )] + = 1
i = 0
while (i < 256 ):
if ((a[i] = = 0 and b[i] = = 0 ) or (a[i] ! = 0 and b[i] ! = 0 )):
i + = 1
else :
return False
a = sorted (a)
b = sorted (b)
for i in range ( 256 ):
if (a[i] ! = b[i]):
return False
return True
if __name__ = = '__main__' :
S1, S2 = "cabbba" , "abbccc"
if (sameStrings(S1, S2)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool sameStrings( string str1,
string str2)
{
int N = str1.Length;
int M = str2.Length;
if (N != M)
{
return false ;
}
int []a = new int [256];
int []b = new int [256];
for ( int j = 0; j < N; j++)
{
a[str1[j] - 'a' ]++;
b[str2[j] - 'a' ]++;
}
int i = 0 ;
while (i < 256)
{
if ((a[i] == 0 && b[i] == 0)
|| (a[i] != 0 && b[i] != 0))
{
i++;
}
else
{
return false ;
}
}
Array.Sort(a);
Array.Sort(b);
for ( int j = 0; j < 256; j++)
{
if (a[j] != b[j])
return false ;
}
return true ;
}
static public void Main()
{
string S1 = "cabbba" , S2 = "abbccc" ;
if (sameStrings(S1, S2))
Console.Write( "YES" + "\n" );
else
Console.Write( " NO" + "\n" );
}
}
|
Javascript
<script>
function sameStrings(str1, str2) {
var N = str1.length;
var M = str2.length;
if (N !== M) {
return false ;
}
var a = new Array(256).fill(0);
var b = new Array(256).fill(0);
for ( var j = 0; j < N; j++) {
a[str1[j].charCodeAt(0) - "a" .charCodeAt(0)]++;
b[str2[j].charCodeAt(0) - "a" .charCodeAt(0)]++;
}
var i = 0;
while (i < 256) {
if ((a[i] === 0 && b[i] === 0) || (a[i] !== 0 && b[i] !== 0)) {
i++;
}
else {
return false ;
}
}
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
for ( var j = 0; j < 256; j++) {
if (a[j] !== b[j]) return false ;
}
return true ;
}
var S1 = "cabbba" ,
S2 = "abbccc" ;
if (sameStrings(S1, S2)) document.write( "YES" + "<br>" );
else document.write( " NO" + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(256)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...