Given two strings contains three characters i.e ‘A’, ‘B ‘and ‘#’ only. Check is it possible to convert first string into another string by performing following operations on string first.
- ‘A’ can move towards Left only
- ‘B’ can move towards Right only
- Neither ‘A’ nor ‘B’ cross each other
If it is possible then print “Yes” otherwise “No”.
Examples:
Input : str1=” #A#B#B# “, str2=” A###B#B ” Output :Yes Explanation : ‘A’ in str1 is right to the ‘A’ in str2 so ‘A’ of str1 can move easily towards the left because there is no ‘B’ on its left positions and for first ‘B’ in str1 is left to the ‘B’ in str2 so ‘B’ of str2 can move easily towards the right because there is no ‘A’ on its right positions and it is same for next ‘B’ so str1 can be easily converted into str2.
Input :str1=” #A#B# “, str2=” #B#A# ” Output :No Explanation : Here first ‘A’ in str1 is left to the ‘A’ in str2 and according to the condition ‘A’ can’tmove towards right. so str1 can’t be converted into str2.
Method :
- Length of Both string must be same
- No. of A’s and B’s in both the strings must be equal
- Order of A and B in both the strings should be same(for ex: if ‘A’ is coming before ‘B’in string second then the same sequence must be follow on string first)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isItPossible(string str1, string str2, int m, int n)
{
if (m != n)
return false ;
if (count(str1.begin(), str1.end(), 'A' ) !=
count(str2.begin(), str2.end(), 'A' ) ||
count(str1.begin(), str1.end(), 'B' ) !=
count(str2.begin(), str2.end(), 'B' ))
return false ;
for ( int i = 0; i < m; i++) {
if (str1[i] != '#' ) {
for ( int j = 0; j < n; j++) {
if ((str2[j] != str1[i]) && str2[j] != '#' )
return false ;
if (str2[j] == str1[i]) {
str2[j] = '#' ;
if (str1[i] == 'A' && i < j)
return false ;
if (str1[i] == 'B' && i > j)
return false ;
break ;
}
}
}
}
return true ;
}
int main()
{
string str1 = "A#B#" ;
string str2 = "A##B" ;
int m = str1.length();
int n = str2.length();
isItPossible(str1, str2, m, n) ? cout << "Yes\n"
: cout << "No\n" ;
return 0;
}
|
Java
class GFG
{
static boolean isItPossible( char [] str1, char [] str2,
int m, int n)
{
if (m != n)
return false ;
if (count(str1, 'A' ) !=
count(str2, 'A' ) ||
count(str1, 'B' ) !=
count(str2, 'B' ))
return false ;
for ( int i = 0 ; i < m; i++) {
if (str1[i] != '#' ) {
for ( int j = 0 ; j < n; j++) {
if ((str2[j] != str1[i]) && str2[j] != '#' )
return false ;
if (str2[j] == str1[i]) {
str2[j] = '#' ;
if (str1[i] == 'A' && i < j)
return false ;
if (str1[i] == 'B' && i > j)
return false ;
break ;
}
}
}
}
return true ;
}
private static int count( char [] str1, char c) {
int count = 0 ;
for ( char temp : str1) {
if (c == temp)
count++;
}
return count;
}
public static void main(String[] args)
{
String str1 = "A#B#" ;
String str2 = "A##B" ;
int m = str1.length();
int n = str2.length();
System.out.print(isItPossible(str1.toCharArray(), str2.toCharArray(), m, n) ?
"Yes\n" : "No\n" );
}
}
|
Python3
def isItPossible(str1, str2, m, n):
if (m ! = n):
return False
if str1.count( 'A' ) ! = str2.count( 'A' ) \
or str1.count( 'B' ) ! = str2.count( 'B' ):
return False
for i in range (m):
if (str1[i] ! = '#' ):
for j in range (n):
if ((str2[j] ! = str1[i]) and str2[j] ! = '#' ):
return False
if (str2[j] = = str1[i]):
str2[j] = '#'
if (str1[i] = = 'A' and i < j):
return False
if (str1[i] = = 'B' and i > j):
return False
break
return True
str1 = "A#B#"
str2 = "A##B"
m = len (str1)
n = len (str2)
str1 = list (str1)
str2 = list (str2)
if (isItPossible(str1, str2, m, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isItPossible( char [] str1, char [] str2,
int m, int n)
{
if (m != n)
return false ;
if (count(str1, 'A' ) !=
count(str2, 'A' ) ||
count(str1, 'B' ) !=
count(str2, 'B' ))
return false ;
for ( int i = 0; i < m; i++) {
if (str1[i] != '#' ) {
for ( int j = 0; j < n; j++) {
if ((str2[j] != str1[i]) && str2[j] != '#' )
return false ;
if (str2[j] == str1[i]) {
str2[j] = '#' ;
if (str1[i] == 'A' && i < j)
return false ;
if (str1[i] == 'B' && i > j)
return false ;
break ;
}
}
}
}
return true ;
}
private static int count( char [] str1, char c) {
int count = 0;
foreach ( char temp in str1) {
if (c == temp)
count++;
}
return count;
}
public static void Main(String[] args)
{
String str1 = "A#B#" ;
String str2 = "A##B" ;
int m = str1.Length;
int n = str2.Length;
Console.Write(isItPossible(str1.ToCharArray(), str2.ToCharArray(), m, n) ?
"Yes\n" : "No\n" );
}
}
|
Javascript
<script>
function getFreq(string,chr) {
let ans = 0;
for ( var i=0; i<string.length;i++) {
if ( chr == string.charAt(i))
ans++;
}
return ans;
};
function isItPossible(str1, str2, m, n){
if (m != n)
return false ;
if (getFreq(str1, 'A' ) !=
getFreq(str2, 'A' ) ||
getFreq(str1, 'B' ) !=
getFreq(str2, 'B' ))
return false ;
for (let i = 0; i < m; i++) {
if (str1[i] != '#' ) {
for (let j = 0; j < n; j++) {
if ((str2[j] != str1[i]) && str2[j] != '#' )
return false ;
if (str2[j] == str1[i]) {
str2 = str2.substr(0,j)+ '#' +str2.substr(j+1);
if (str1[i] == 'A' && i < j)
return false ;
if (str1[i] == 'B' && i > j)
return false ;
break ;
}
}
}
}
return true ;
}
let str1 = "A#B#" ;
let str2 = "A##B" ;
let m = str1.length;
let n = str2.length;
isItPossible(str1, str2, m, n) ?document.write( "Yes<br>" )
: document.write( "No<br>" );
</script>
|
Time Complexity : O(n*m) Auxiliary Space: O(n+m)
|