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.

In this problem, we have two strings where characters 'A' can only move left, characters 'B' can only move right, and empty spaces are represented by '#'. The key constraint is that 'A' and 'B' characters cannot cross over each other during movement.

Syntax

bool moveRobots(char str1[], char str2[]);

Examples

Example 1

Input: str1 = "#B#A#", str2 = "##BA#"
Output: Yes

Explanation 'B' shifts one space to the right. The string changes to "##BA#", which matches str2.

Example 2

Input: str1 = "#A#B#", str2 = "#B#A#" 
Output: No

Explanation 'A' cannot move right and 'B' cannot move left, so transformation is impossible.

Algorithm

The algorithm follows these steps

  • Step 1 Remove all '#' characters from both strings and compare if they are identical
  • Step 2 Store positions of non-'#' characters in both strings
  • Step 3 For each 'A': position in str2 must be ? position in str1 (can only move left)
  • Step 4 For each 'B': position in str2 must be ? position in str1 (can only move right)
  • Step 5 Return true if all conditions are satisfied

Example Program

#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;
    
    // Remove '#' from str1
    for (int i = 0; str1[i] != '\0'; i++) {
        if (str1[i] != '#')
            a1[a1Index++] = str1[i];
    }
    a1[a1Index] = '\0';
    
    // Remove '#' from str2
    for (int i = 0; str2[i] != '\0'; i++) {
        if (str2[i] != '#')
            a2[a2Index++] = str2[i];
    }
    a2[a2Index] = '\0';
    
    // Check if strings without '#' are identical
    if (strcmp(a1, a2) != 0) {
        return false;
    }
    
    // Store positions of non-'#' characters
    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;
    }
    
    // Check movement constraints
    int n = strlen(a1);
    for (int i = 0; i < n; i++) {
        if (a1[i] == 'A') {
            // 'A' can only move left (position should decrease or stay same)
            if (v1[i] < v2[i])
                return false;
        } else if (a1[i] == 'B') {
            // 'B' can only move right (position should increase or stay same)
            if (v1[i] > v2[i])
                return false;
        }
    }
    
    return true;
}

int main() {
    char str1[] = "#B#A#";
    char str2[] = "##BA#";
    
    printf("str1: %s<br>", str1);
    printf("str2: %s<br>", str2);
    
    if (moveRobots(str1, str2))
        printf("Result: Yes<br>");
    else
        printf("Result: No<br>");
        
    return 0;
}
str1: #B#A#
str2: ##BA#
Result: Yes

How It Works

The algorithm first removes all '#' characters and checks if the remaining characters are identical in both strings. Then it verifies movement constraints: 'A' characters can only move to positions with smaller or equal indices (leftward), while 'B' characters can only move to positions with larger or equal indices (rightward).

Conclusion

This solution efficiently determines if string transformation is possible by checking character sequence equality and position movement constraints. The time complexity is O(n) where n is the string length.

Updated on: 2026-03-15T14:32:29+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements