Convert a string Str1 to Str2 by moving B to right and A to left without crossover


The aim of this article is to implement a program to convert a string Str1 to Str2 by moving B to right and A to left without crossover.

As we know, a string is a group of characters that ends with the null character "\0" in C programming. Characters from the C String are kept in a character array. A C string differs from a character array in that it ends with the distinctive character "\0".

Example

Let us take the input strings, str1 = “#B#A#”, and str2 = “##BA#”
Output obtained here is: Yes

Explanation − 'B' shifts one space to the right in a single step. The string thus changes to "##BA#," which is identical to the final s2 string.

Example

Let us take the input strings, str1 = “#A#B#”, and str2 = “#B#A#”
Output obtained here is : Yes

Problem Statement

Implement a program to convert a string Str1 to Str2 by moving B to right and A to left without crossover

Approach

According to the above observation, the concept for solving the issue is as follows:

When the strings meet the requirement, the robots can move to their ultimate location.

Since crossover of A and B is not permitted, both strings ought to be identical provided the empty spaces '#' are deleted.

If the positions of 'A' are different in s2 from s1 and 'B' are different in s2 from s1 after the '#' has been removed from both strings.

Algorithm

The algorithm for the program to convert a string S1 to S2 by moving B to right and A to left without crossovergiven below −

  • Step 1 − implement a Function to check whether the robots can move or not

  • Step 2 − define array a1 and a2 inorder for saving strings both str1 and str2 with no //use of the '#'.

  • Step 3 − checking the first condition− that is, the strings s1 and s2 must exactly match each other without any gaps.

  • Step 4 − The relative positions for 'A' or 'B' in str1 as well as str2 will be stored in v1 and v2, respectively.

  • Step 5 − Checking the Condition 2− that is, the positions of 'A' in the string str1 and 'B' in the string str2 ought to be larger compared to or equal to each other, and the positions of 'A' and 'B' should be below or equivalent to each other.

  • Step 6 − Print the result as output.

Example (C Program)

Here is the C program implementation of the above written algorithm to convert string S1 to S2 by moving B to right and A to left without crossover.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool moveRobots(char str1[], char str2[]) {
   char a1[100], a2[100];
   int a1Index = 0, a2Index = 0;
   for (int i = 0; str1[i] != '\0'; i++) {
      if (str1[i] != '#')
         a1[a1Index++] = str1[i];
   }
   a1[a1Index] = '\0';
   for (int i = 0; str2[i] != '\0'; i++) {
      if (str2[i] != '#')
         a2[a2Index++] = str2[i];
   }
   a2[a2Index] = '\0';
   if (strcmp(a1, a2) == 0) {
      int n = strlen(a1);
      int v1[100], v2[100];
      int v1Index = 0, v2Index = 0;
      for (int i = 0; str1[i] != '\0'; i++) {
         if (str1[i] != '#')
            v1[v1Index++] = i;
      }
      for (int i = 0; str2[i] != '\0'; i++) {
         if (str2[i] != '#')
            v2[v2Index++] = i;
      }
      if (a1[0] == 'A' && v1[0] < v2[0])
         return false;
      if (a1[0] == 'B' && v1[0] > v2[0])
         return false;
      for (int i = 1; i < n; i++) {
         if (a1[i] == 'A') {
            if (v1[i] < v2[i])
               return false;
         } else {
            if (v1[i] > v2[i])
               return false;
         }
      }
      return true;
   }
   return false;
}
int main() {
   char str1[] = "#B#A#";
   char str2[] = "##BA#";
   if (moveRobots(str1, str2))
      printf("Yes
"); else printf("No
"); return 0; }

Output

On execution, it will produce the following output −

Yes

Conclusion

Likewise, we can convert a string S1 to S2 by moving B to right and A to left without crossover. The challenge of obtaining the prpgram to convert string S1 to S2 by moving B to right and A to left without crossover is resolved in this article.

Here C programming code as well as the algorithm and methodology to implement a program to convert string S1 to S2 by moving B to right and A to left without crossover are provided.

Updated on: 31-Oct-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements