通过将 B 向右移动和 A 向左移动而不交叉将字符串S1 转换为 S2
给定由'A'、'B' 和 '#'组成的字符串s1和s2 ,其中:
- '#' 代表一个空单元格
- “A”代表可以向左移动的机器人
- “B”代表可以朝正确方向移动的机器人
任务是检查是否可以通过机器人的移动将 s1 转换为 s2。
例子:
Input: s1 = “#B#A#”, s2 = “##BA#”
Output: Yes
Explanation: In one step ‘B’ moves one position to the right.
So the string becomes “##BA#” which is same as the final s2 string
Input: s1 = “#B#A#”, s2 = “#A#B#”
Output: No
Explanation: As the robots cannot cross each other.
方法:解决问题的思路是基于 以下观察:
The robots can reach the final position when the strings satisfy the condition:
- If the empty spaces ‘#’ are removed from string then both the strings should be identical as no crossing of A and B are allowed.
- After removing the ‘#’ from both the strings, if positions of A is lesser in s2 than in s1 and positions of ‘B’ is greater in s2 than in s1.
按照下面提到的步骤来实现上述观察:
- 从头开始迭代数组:
- 从两个字符串中删除空格 ( '#' )。
- 将A和B的位置存储在两个字符串中
- 如果修改后的字符串s1 和 s2 完全相同,那么它们可以在以下情况下达到最终状态:
- 'A'在s1中的位置大于或等于'A'在s2中的位置, 'B'在s1中的位置小于或等于'B'在s2中的位置。
- 在所有其他情况下,机器人无法到达s2中提到的最终位置。
以下是上述方法的实现。
C++
// C++ code for the above approach:
#include
using namespace std;
// Function to check if robots can move
string moveRobots(string s1, string s2)
{
// Strings to save s1 and s2 without '#'
string a, b;
for (int i = 0; i < s1.size(); i++)
if (s1[i] != '#')
a += s1[i];
for (int i = 0; i < s2.size(); i++)
if (s2[i] != '#')
b += s2[i];
// Condition 1: strings s1 and s2
// without empty spaces should be
// exactly same
if (a == b) {
int n = a.size();
// v1 and v2 will store the
// positions of 'A' or 'B'
// in s1 and s2 respectively
vector v1;
vector v2;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] != '#')
v1.push_back(i);
}
for (int i = 0; i < s2.size(); i++)
if (s2[i] != '#')
v2.push_back(i);
// Condition 2:
// Position of 'A' in s1 should be
// greater than or equal to
// Position of 'A' in s2 and
// Position of 'B' in s1 should be
// less than or equal to
// Position of 'B' in s2
if (a[0] == 'A' and v1[0] < v2[0])
return "No";
if (a[0] == 'B' and v1[0] > v2[0])
return "No";
for (int i = 1; i < n; i++) {
if (a[i] == 'A') {
if (v1[i] < v2[i])
return "No";
}
else {
if (v1[i] > v2[i])
return "No";
}
}
return "Yes";
}
return "No";
}
// Driver code
int main()
{
string s1 = "#B#A#";
string s2 = "##BA#";
cout << moveRobots(s1, s2) << endl;
return 0;
}
Java
// Java code for the above approach:
import java.util.*;
class GFG {
// Function to check if robots can move
static String moveRobots(String s1, String s2)
{
// Strings to save s1 and s2 without '#'
String a = "", b = "";
for (int i = 0; i < s1.length(); i++)
if (s1.charAt(i) != '#')
a += s1.charAt(i);
for (int i = 0; i < s2.length(); i++)
if (s2.charAt(i) != '#')
b += s2.charAt(i);
// Condition 1: strings s1 and s2
// without empty spaces should be
// exactly same
if (a.equals(b)) {
int n = a.length();
// v1 and v2 will store the
// positions of 'A' or 'B'
// in s1 and s2 respectively
ArrayList v1
= new ArrayList();
ArrayList v2
= new ArrayList();
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != '#')
v1.add(i);
}
for (int i = 0; i < s2.length(); i++)
if (s2.charAt(i) != '#')
v2.add(i);
// Condition 2:
// Position of 'A' in s1 should be
// greater than or equal to
// Position of 'A' in s2 and
// Position of 'B' in s1 should be
// less than or equal to
// Position of 'B' in s2
if (a.charAt(0) == 'A'
&& (int)v1.get(0) < (int)v2.get(0))
return "No";
if (a.charAt(0) == 'B'
&& (int)v1.get(0) > (int)v2.get(0))
return "No";
for (int i = 1; i < n; i++) {
if (a.charAt(i) == 'A') {
if ((int)v1.get(i) < (int)v2.get(i))
return "No";
}
else {
if ((int)v1.get(i) > (int)v2.get(i))
return "No";
}
}
return "Yes";
}
return "No";
}
// Driver code
public static void main(String[] args)
{
String s1 = "#B#A#";
String s2 = "##BA#";
System.out.println(moveRobots(s1, s2));
}
}
// This code is contributed by ukasp.
Python3
# python3 code for the above approach:
# Function to check if robots can move
def moveRobots(s1, s2):
# Strings to save s1 and s2 without '#'
a, b = "", ""
for i in range(0, len(s1)):
if (s1[i] != '#'):
a += s1[i]
for i in range(0, len(s2)):
if (s2[i] != '#'):
b += s2[i]
# Condition 1: strings s1 and s2
# without empty spaces should be
# exactly same
if (a == b):
n = len(a)
# v1 and v2 will store the
# positions of 'A' or 'B'
# in s1 and s2 respectively
v1 = []
v2 = []
for i in range(0, len(s1)):
if (s1[i] != '#'):
v1.append(i)
for i in range(0, len(s2)):
if (s2[i] != '#'):
v2.append(i)
# Condition 2:
# Position of 'A' in s1 should be
# greater than or equal to
# Position of 'A' in s2 and
# Position of 'B' in s1 should be
# less than or equal to
# Position of 'B' in s2
if (a[0] == 'A' and v1[0] < v2[0]):
return "No"
if (a[0] == 'B' and v1[0] > v2[0]):
return "No"
for i in range(1, n):
if (a[i] == 'A'):
if (v1[i] < v2[i]):
return "No"
else:
if (v1[i] > v2[i]):
return "No"
return "Yes"
return "No"
# Driver code
if __name__ == "__main__":
s1 = "#B#A#"
s2 = "##BA#"
print(moveRobots(s1, s2))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach:
using System;
using System.Collections;
class GFG
{
// Function to check if robots can move
static string moveRobots(string s1, string s2)
{
// Strings to save s1 and s2 without '#'
string a = "", b = "";
for (int i = 0; i < s1.Length; i++)
if (s1[i] != '#')
a += s1[i];
for (int i = 0; i < s2.Length; i++)
if (s2[i] != '#')
b += s2[i];
// Condition 1: strings s1 and s2
// without empty spaces should be
// exactly same
if (a == b) {
int n = a.Length;
// v1 and v2 will store the
// positions of 'A' or 'B'
// in s1 and s2 respectively
ArrayList v1 = new ArrayList();
ArrayList v2 = new ArrayList();
for (int i = 0; i < s1.Length; i++) {
if (s1[i] != '#')
v1.Add(i);
}
for (int i = 0; i < s2.Length; i++)
if (s2[i] != '#')
v2.Add(i);
// Condition 2:
// Position of 'A' in s1 should be
// greater than or equal to
// Position of 'A' in s2 and
// Position of 'B' in s1 should be
// less than or equal to
// Position of 'B' in s2
if (a[0] == 'A' && (int)v1[0] < (int)v2[0])
return "No";
if (a[0] == 'B' && (int)v1[0] > (int)v2[0])
return "No";
for (int i = 1; i < n; i++) {
if (a[i] == 'A') {
if ((int)v1[i] < (int)v2[i])
return "No";
}
else {
if ((int)v1[i] > (int)v2[i])
return "No";
}
}
return "Yes";
}
return "No";
}
// Driver code
public static void Main()
{
string s1 = "#B#A#";
string s2 = "##BA#";
Console.Write(moveRobots(s1, s2));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Yes
时间复杂度: O(N)
辅助空间: O(N)