Tuesday, November 26, 2024
Google search engine
HomeData Modelling & AICheck if it is possible to convert one string into another with...

Check if it is possible to convert one string into another with given constraints

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. 

  1. ‘A’ can move towards Left only 
  2. ‘B’ can move towards Right only 
  3. 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 : 

  1. Length of Both string must be same 
  2. No. of A’s and B’s in both the strings must be equal 
  3. 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++




// C++ Program for above implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to check is it possible to convert
// first string into another string or not.
bool isItPossible(string str1, string str2, int m, int n)
{
 
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
 
    // To Check  Frequency of A's and  B's are
    // equal in both strings or not.
    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;
 
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
 
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
 
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
 
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
 
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    if (str1[i] == 'B' && i > j)
                        return false;
 
                    break;
                }
            }
        }
    }
 
    return true;
}
 
// Drivers code
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




// Java Program for above implementation
class GFG
{
 
// Function to check is it possible to convert
// first String into another String or not.
static boolean isItPossible(char[] str1, char[] str2,
                            int m, int n)
{
 
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
 
    // To Check Frequency of A's and B's are
    // equal in both Strings or not.
    if (count(str1, 'A') !=
        count(str2, 'A') ||
        count(str1, 'B') !=
            count(str2, 'B'))
        return false;
 
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
 
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
 
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
 
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
 
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    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;
}
 
// Drivers code
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");
 
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python Program for above implementation
 
# Function to check is it possible to convert
# first string into another string or not.
def isItPossible(str1, str2, m, n):
 
    # To Check Length of Both String is Equal or Not
    if (m != n):
        return False
 
    # To Check Frequency of A's and B's are
    # equal in both strings or not.
    if str1.count('A') != str2.count('A') \
    or str1.count('B') != str2.count('B'):
        return False
 
    # Start traversing
    for i in range(m):
        if (str1[i] != '#'):
            for j in range(n):
                # To Check no two elements cross each other.
                if ((str2[j] != str1[i]) and str2[j] != '#'):
                    return False
 
                if (str2[j] == str1[i]):
                    str2[j] = '#'
 
                    # To Check Is it Possible to Move
                    # towards Left or not.
                    if (str1[i] == 'A' and i < j):
                        return False
 
                    # To Check Is it Possible to Move
                    # towards Right or not.
                    if (str1[i] == 'B' and i > j):
                        return False
 
                    break
                 
    return True
 
# Drivers code
 
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")
 
# This code is contributed by ankush_953


C#




// C# Program for above implementation
using System;
 
class GFG
{
  
// Function to check is it possible to convert
// first String into another String or not.
static bool isItPossible(char[] str1, char[] str2,
                            int m, int n)
{
  
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
  
    // To Check Frequency of A's and B's are
    // equal in both Strings or not.
    if (count(str1, 'A') !=
        count(str2, 'A') ||
        count(str1, 'B') !=
            count(str2, 'B'))
        return false;
  
    // Start traversing
    for (int i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (int j = 0; j < n; j++) {
  
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
  
                if (str2[j] == str1[i]) {
                    str2[j] = '#';
  
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
  
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    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;
}
  
// Drivers code
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");
  
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// js Program for above implementation
function getFreq(string,chr) {
  let ans = 0;
    for (var i=0; i<string.length;i++) {
        if( chr == string.charAt(i))
          ans++; 
    }
    return ans;
};
// Function to check is it possible to convert
// first string into another string or not.
function isItPossible(str1, str2, m, n){
    // To Check Length of Both String is Equal or Not
    if (m != n)
        return false;
    // To Check  Frequency of A's and  B's are
    // equal in both strings or not.
    if (getFreq(str1, 'A') !=
           getFreq(str2, 'A') ||
        getFreq(str1, 'B') !=
            getFreq(str2, 'B'))
        return false;
 
    // Start traversing
    for (let i = 0; i < m; i++) {
        if (str1[i] != '#') {
            for (let j = 0; j < n; j++) {
 
                // To Check no two elements cross each other.
                if ((str2[j] != str1[i]) && str2[j] != '#')
                    return false;
 
                if (str2[j] == str1[i]) {
                    str2 = str2.substr(0,j)+'#'+str2.substr(j+1);
 
                    // To Check Is it Possible to Move
                    // towards Left or not.
                    if (str1[i] == 'A' && i < j)
                        return false;
 
                    // To Check Is it Possible to Move
                    // towards Right or not.
                    if (str1[i] == 'B' && i > j)
                        return false;
 
                    break;
                }
            }
        }
    }
 
    return true;
}
 
// Drivers code
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>


Output

Yes

Time Complexity : O(n*m)
Auxiliary Space: O(n+m)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments