给定两个二进制字符串A和B ,任务是查找从A的第一个字符开始的子字符串需要翻转的最小次数,即将1 s转换为0 s,将0 s转换为1 s,以将A转换为B.
例子:
Input: A = “0010”, B = “1011”
Output; 3
Explanation:
Step 1: Flip the entire string A. Therefore, A becomes “1101” .
Step 2: Flip the substring {A[0], A[2]}. Therefore, A becomes “0011” .
Step 3: Flip A[0]. Therefore, A becomes “1011” which is equal to B.
Therefore, the minimum number of operations required is 3.
Input: A = “1010101”, B = “0011100”
Output: 5
Explanation:
Step 1: Flip the entiThehrefore, A becomes “0101010″
Step 2: Flip substring {A[0], A[5]}. Therefore, A becomes “1010100″
Step 3: Flip the substring {A[0], A[3]}. Therefore, A becomes “0101100″
Step 4: Flip the substring {A[0], A[2]}. Therefore, A becomes “1011100″
Step 5: Flip A[0]. Therefore, A becomes “0011100” which is equal to B.
Therefore, the minimum number of operations required is 5.
的方法:我们的想法是初始化,保持最后索引在该字符在A是从在B点的字符不同的变量。然后从第一个索引到最后一个索引取反A。重复直到两个字符串相等。请按照以下步骤解决问题:
- 初始化一个变量last_index ,该变量包含A和B中字符不同的最后一个索引。
- 将字符串A从第一个索引取反到last_index,并增加步数。
- 重复上述步骤,直到字符串A等于字符串B为止。
- 执行完两个字符串相同的字符串后,打印步数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
void findMinimumOperations(string a,
string b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index;
// Iterate until both string
// are unequal
while (a != b) {
// Check till end of string to
// find righmost unequals bit
for (int i = 0;
i < a.length(); i++) {
// Update the last index
if (a[i] != b[i]) {
last_index = i;
}
}
// Flipping characters up
// to the last index
for (int i = 0;
i <= last_index; i++) {
// Flip the bit
a[i] = (a[i] == '0') ? '1' : '0';
}
// Increasing steps by one
step++;
}
// Print the count of steps
cout << step;
}
// Driver Code
int main()
{
// Given strings A and B
string A = "101010", B = "110011";
// Function Call
findMinimumOperations(A, B);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function that finds the minimum
// number of operations required such
// that String A and B are the same
static void findMinimumOperations(char[] a,
char[] b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index = 0;
// Iterate until both String
// are unequal
while (!Arrays.equals(a, b))
{
// Check till end of String to
// find righmost unequals bit
for (int i = 0;
i < a.length; i++)
{
// Update the last index
if (a[i] != b[i])
{
last_index = i;
}
}
// Flipping characters up
// to the last index
for (int i = 0;
i <= last_index; i++)
{
// Flip the bit
a[i] = (a[i] == '0') ?
'1' : '0';
}
// Increasing steps by one
step++;
}
// Print the count of steps
System.out.print(step);
}
// Driver Code
public static void main(String[] args)
{
// Given Strings A and B
String A = "101010",
B = "110011";
// Function Call
findMinimumOperations(A.toCharArray(),
B.toCharArray());
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function that finds the minimum
# number of operations required such
# that string A and B are the same
def findMinimumOperations(a, b):
# Stores the count of steps
step = 0
# Stores the last index whose
# bits are not same
last_index = 0
# Iterate until both string
# are unequal
while (a != b):
a = [i for i in a]
# Check till end of string to
# find righmost unequals bit
for i in range(len(a)):
# Update the last index
if (a[i] != b[i]):
last_index = i
# Flipping characters up
# to the last index
for i in range(last_index + 1):
# Flip the bit
if (a[i] == '0'):
a[i] = '1'
else:
a[i] = '0'
a = "".join(a)
# Increasing steps by one
step += 1
# Print the count of steps
print(step)
# Driver Code
if __name__ == '__main__':
# Given strings A and B
A = "101010"
B = "110011"
# Function Call
findMinimumOperations(A, B)
# This code is contributed by mohit kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
static void findMinimumOperations(string a,
string b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index = 0;
// Iterate until both string
// are unequal
while (a.Equals(b) == false)
{
// Check till end of string to
// find righmost unequals bit
for(int i = 0; i < a.Length; i++)
{
// Update the last index
if (a[i] != b[i])
{
last_index = i;
}
}
// Flipping characters up
// to the last index
char[] ch = a.ToCharArray();
for(int i = 0;
i <= last_index; i++)
{
// Flip the bit
if (ch[i] == '0')
{
ch[i] = '1';
}
else
{
ch[i] = '0';
}
}
a = new string(ch);
// Increasing steps by one
step++;
}
// Print the count of steps
Console.WriteLine(step);
}
// Driver Code
public static void Main()
{
// Given strings A and B
string A = "101010";
string B = "110011";
// Function Call
findMinimumOperations(A,B);
}
}
// This code is contributed by SURENDRA_GANGWAR
4
时间复杂度: O(N 2 )
辅助空间: O(1)