仅使用否定将一个二进制字符串转换为其他二进制字符串的最小步骤
给定两个二进制字符串A和B ,任务是通过选择 A 的任何子字符串并将其取反(将每个 0 替换为 1 并将每个 1 替换为 0)将 A 转换为 B。打印所需的最少操作数。
例子:
Input: A = “101010”, B = “110011”
Output: 2
Choose the sub-string of length 2 from index 1 to 2 and negate it then A becomes “110010” and then take the last character and negate it.
The final string becomes “110011”
Input: A = “1010101”, B = “0011100”
Output: 3
方法:创建一个空白数组并标记需要取反的索引。那么答案将是数组中连续 1 的块数,因为单个块可以在单个操作中被否定。
例如,A = “101010”,B = “110011”
新创建的数组将是 {0, 1, 1, 0, 0, 1} 所以答案是 2,
第一次操作后将是“110010”
第二次操作“110011”后
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the minimum steps
// to convert string a to string b
void convert(int n, string a, string b)
{
// array to mark the positions
// needed to be negated
int l[n];
int i;
for (i = 0; i < n; i++)
l[i] = 0;
for (i = 0; i < n; i++) {
// If two character are not same
// then they need to be negated
if (a[i] != b[i])
l[i] = 1;
}
// To count the blocks of 1
int cc = 0;
// To count the number of 1's in
// each block of 1's
int vl = 0;
for (i = 0; i < n; i++) {
if (l[i] == 0) {
if (vl != 0)
cc += 1;
vl = 0;
}
else
vl += 1;
}
// For the last block of 1's
if (vl != 0)
cc += 1;
cout << cc << endl;
}
// Driver code
int main()
{
string a = "101010";
string b = "110011";
int n = a.length();
convert(n, a, b);
return 0;
}
// This code is contributed by ANKITRAI1
Java
// Java implementation of the above approach
import java.util.*;
class solution {
// Function to find the minimum steps
// to convert string a to string b
static void convert(int n, String a, String b)
{
// array to mark the positions
// needed to be negated
int[] l = new int[n];
int i;
for (i = 0; i < n; i++)
l[i] = 0;
for (i = 0; i < n; i++) {
// If two character are not same
// then they need to be negated
if (a.charAt(i) != b.charAt(i))
l[i] = 1;
}
// To count the blocks of 1
int cc = 0;
// To count the number of 1's in
// each block of 1's
int vl = 0;
for (i = 0; i < n; i++) {
if (l[i] == 0) {
if (vl != 0)
cc += 1;
vl = 0;
}
else
vl += 1;
}
// For the last block of 1's
if (vl != 0)
cc += 1;
System.out.println(cc);
}
// Driver code
public static void main(String args[])
{
String a = "101010";
String b = "110011";
int n = a.length();
convert(n, a, b);
}
}
Python3
# Python3 implementation of the above approach
# Function to find the minimum steps
# to convert string a to string b
def convert(n, a, b):
# List to mark the positions needed to
# be negated
l = [0] * n
for i in range(n):
# If two character are not same
# then they need to be negated
if(a[i] != b[i]):
l[i] = 1
# To count the blocks of 1
cc = 0
# To count the number of 1's in each
# block of 1's
vl = 0
for i in range(n):
if (l[i] == 0):
if(vl != 0):
cc += 1
vl = 0
else:
vl += 1
# For the last block of 1's
if(vl != 0):
cc += 1
print(cc)
# Driver code
a = "101010"
b = "110011"
n = len(a)
convert(n, a, b)
C#
// C# implementation of the above approach
using System;
class GFG {
// Function to find the minimum steps
// to convert string a to string b
static void convert(int n, String a, String b)
{
// array to mark the positions
// needed to be negated
int[] l = new int[n];
int i;
for (i = 0; i < n; i++)
l[i] = 0;
for (i = 0; i < n; i++) {
// If two character are not same
// then they need to be negated
if (a[i] != b[i])
l[i] = 1;
}
// To count the blocks of 1
int cc = 0;
// To count the number of 1's in
// each block of 1's
int vl = 0;
for (i = 0; i < n; i++) {
if (l[i] == 0) {
if (vl != 0)
cc += 1;
vl = 0;
}
else
vl += 1;
}
// For the last block of 1's
if (vl != 0)
cc += 1;
Console.WriteLine(cc);
}
// Driver code
static public void Main()
{
String a = "101010";
String b = "110011";
int n = a.Length;
convert(n, a, b);
}
}
// This code is contributed by jit_t.
PHP
Javascript
输出:
2