通过给定的操作检查 str1 是否可以转换为 str2
给定两个二进制字符串str1和str2 。任务是检查是否可以通过在 str1 上应用以下操作任意次数来将str1转换为str2 。
在每个操作中,可以将两个连续的 0 组合成一个1 。
例子:
Input: str1 = “00100”, str2 = “111”
Output: Yes
Combine first two zeros to one and combine last two zeros to one.
Input: str1 = “00”, str2 = “000”
Output: No
It is not possible to convert str1 to str2.
方法:让我们从左到右逐个字符字符并行处理 str1 和 str2。让我们使用两个索引 i 和 j:索引 i 用于 str1,索引 j 用于 str2。现在,有两种情况:
- 如果str1[i] = str2[j]然后增加i和j 。
- 如果str1[i] != str2[j]那么,
- 如果 str1 中有两个连续的 0,即str1[i] = 0和str1[i + 1] = 0和str2[j] = 1 ,这意味着可以将两个 0 组合以匹配str2中的1 。因此,将i增加2并将j增加1 。
- 否则str1无法转换为str2 。
- 如果最后i和j都位于它们各自字符串的末尾,即str1和str2 ,那么答案是Yes否则答案是No 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true if str1 can be
// converted to str2 with the given operations
bool canConvert(string str1, string str2)
{
int i = 0, j = 0;
// Traverse from left to right
while (i < str1.size() && j < str2.size()) {
// If the two characters do not match
if (str1[i] != str2[j]) {
// If possible to combine
if (str1[i] == '0' && str2[j] == '1'
&& i + 1 < str1.size()
&& str1[i + 1] == '0') {
i += 2;
j++;
}
// If not possible to combine
else {
return false;
}
}
// If the two characters match
else {
i++;
j++;
}
}
// If possible to convert one string to another
if (i == str1.size() && j == str2.size())
return true;
return false;
}
// Driver code
int main()
{
string str1 = "00100", str2 = "111";
if (canConvert(str1, str2))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that returns true if str1 can be
// converted to str2 with the given operations
static boolean canConvert(String str1, String str2)
{
int i = 0, j = 0;
// Traverse from left to right
while (i < str1.length() && j < str2.length())
{
// If the two characters do not match
if (str1.charAt(i) != str2.charAt(j))
{
// If possible to combine
if (str1.charAt(i) == '0' && str2.charAt(j) == '1'
&& i + 1 < str1.length() && str1.charAt(i+1) == '0')
{
i += 2;
j++;
}
// If not possible to combine
else
{
return false;
}
}
// If the two characters match
else
{
i++;
j++;
}
}
// If possible to convert one string to another
if (i == str1.length() && j == str2.length())
return true;
return false;
}
// Driver code
public static void main(String[] args)
{
String str1 = "00100", str2 = "111";
if (canConvert(str1, str2))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code contributed by Rajput-Ji
Python3
# Python implementation of the approach
# Function that returns true if str1 can be
# converted to str2 with the given operations
def canConvert(str1, str2):
i, j = 0, 0;
# Traverse from left to right
while (i < len(str1) and j < len(str2)):
# If the two characters do not match
if (str1[i] != str2[j]):
# If possible to combine
if (str1[i] == '0' and str2[j] == '1'
and i + 1 < len(str1)
and str1[i + 1] == '0'):
i += 2;
j+=1;
# If not possible to combine
else:
return False;
# If the two characters match
else:
i += 1;
j += 1;
# If possible to convert one string to another
if (i == len(str1) and j == len(str2)):
return True;
return False;
# Driver code
str1 = "00100";
str2 = "111";
if (canConvert(str1, str2)):
print("Yes");
else:
print("No");
# This code is contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that returns true if str1 can be
// converted to str2 with the given operations
static bool canConvert(string str1, string str2)
{
int i = 0, j = 0;
// Traverse from left to right
while (i < str1.Length && j < str2.Length)
{
// If the two characters do not match
if (str1[i] != str2[j])
{
// If possible to combine
if (str1[i] == '0' && str2[j] == '1'
&& i + 1 < str1.Length && str1[i+1] == '0')
{
i += 2;
j++;
}
// If not possible to combine
else
{
return false;
}
}
// If the two characters match
else
{
i++;
j++;
}
}
// If possible to convert one string to another
if (i == str1.Length && j == str2.Length)
return true;
return false;
}
// Driver code
public static void Main()
{
string str1 = "00100", str2 = "111";
if (canConvert(str1, str2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
Yes