给定两个长度为N 的二进制字符串A和B ,任务是通过重复翻转A的前缀的所有位,将字符串从A转换为字符串B ,即将前缀中的所有0转换为1 ,反之亦然。反之,并打印所需的最小前缀翻转次数和相应前缀的长度。
例子:
Input: A = “001”, B = “000”
Output:
2
3 2
Explanation:
Flipping the prefix “001” modifies the string to “110”.
Flipping the prefix “11” modifies the string to “000”.
Input: A = “1000”, B = “1011”
Output:
2
4 2
Explanation:
Flipping the prefix “1000” modifies the string to “0111”.
Flipping the prefix “01” modifies the string to “1011”.
朴素的方法:最简单的方法是反向遍历字符串A ,对于获得的每个第i个字符,使得A[i]不等于B[i] ,从索引[0, i]和翻转A 中存在的字符将操作数增加1 。完成字符串遍历后,打印操作计数和每个操作中选择的前缀长度。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:这个想法是通过反向遍历字符串A一次修复一位。维护一个布尔变量,比如invert ,最初设置为false ,以表示A中的当前位是否被翻转。遍历时,执行以下操作:
- 如果A 中的iᵗʰ位不等于B 中的iᵗʰ位并且invert为false ,则增加操作计数并将invert设置为true 。
- 否则,如果A中的iᵗʰ位等于B中的iᵗʰ位和反转为真,则增加操作的数量和设置反转为false。
请按照以下步骤解决问题:
- 初始化一个布尔变量,比如invert为false,以表示A中的位是否被翻转。
- 初始化一个空数组,比如res,以存储每个操作中的前缀长度。
- 使用变量i在范围[N – 1, 0] 中迭代并执行以下步骤:
- 如果A[i] != B[i]并且invert为false ,则需要翻转当前位。所以。将(i + 1)插入数组res并将invert更新为true 。
- 否则,检查A[i] == B[i]和invert是否为true ,然后将(i + 1)插入res ,并将invert更新为false 。
- 将数组res的大小打印为使两个字符串相等所需的操作数。
- 然后,打印res 中存储的值以表示每个操作中的前缀长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count flips required
// to make strings A and B equal
void findOperations(string A,
string B, int N)
{
// Stores the count of the
// number of operations
int operations = 0;
// Stores the length of
// the chosen prefixes
vector ops;
// Stores if operations are
// performed even or odd times
bool invert = false;
// Traverse the given string
for (int i = N - 1; i >= 0; i--) {
// If current characters in
// the two strings are unequal
if (A[i] != B[i]) {
// If A[i] is not flipped
if (!invert) {
// Increment count
// of operations
operations++;
// Insert the length of
// the chosen prefix
ops.push_back(i + 1);
// Update invert to true
invert = true;
}
}
else {
// If A[i] is flipped
if (invert) {
// Increment count
// of operations
operations++;
// Insert length of
// the chosen prefix
ops.push_back(i + 1);
// Update invert to false
invert = false;
}
}
}
// Print the number of
// operations required
cout << operations << endl;
// Print the chosen prefix
// length in each operation
if (operations != 0) {
for (auto x : ops)
cout << x << " ";
}
}
// Driver Code
int main()
{
// Given binary strings
string A = "001", B = "000";
int N = A.size();
findOperations(A, B, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count flips required
// to make Strings A and B equal
static void findOperations(String A,
String B, int N)
{
// Stores the count of the
// number of operations
int operations = 0;
// Stores the length of
// the chosen prefixes
Vector ops = new Vector<>();
// Stores if operations are
// performed even or odd times
boolean invert = false;
// Traverse the given String
for (int i = N - 1; i >= 0; i--) {
// If current characters in
// the two Strings are unequal
if (A.charAt(i) != B.charAt(i)) {
// If A[i] is not flipped
if (!invert) {
// Increment count
// of operations
operations++;
// Insert the length of
// the chosen prefix
ops.add(i + 1);
// Update invert to true
invert = true;
}
}
else {
// If A[i] is flipped
if (invert) {
// Increment count
// of operations
operations++;
// Insert length of
// the chosen prefix
ops.add(i + 1);
// Update invert to false
invert = false;
}
}
}
// Print the number of
// operations required
System.out.print(operations +"\n");
// Print the chosen prefix
// length in each operation
if (operations != 0) {
for (int x : ops)
System.out.print(x+ " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given binary Strings
String A = "001", B = "000";
int N = A.length();
findOperations(A, B, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python program for the above approach
# Function to count flips required
# to make strings A and B equal
def findOperations(A, B, N):
# Stores the count of the
# number of operations
operations = 0
# Stores the length of
# the chosen prefixes
ops = []
# Stores if operations are
# performed even or odd times
invert = False
# Traverse the given string
for i in range(N - 1, -1, -1):
# If current characters in
# the two strings are unequal
if (A[i] != B[i]):
# If A[i] is not flipped
if (not invert):
# Increment count
# of operations
operations += 1
# Insert the length of
# the chosen prefix
ops.append(i + 1)
# Update invert to true
invert = True
else:
# If A[i] is flipped
if (invert):
# Increment count
# of operations
operations += 1
# Insert length of
# the chosen prefix
ops.append(i + 1)
# Update invert to false
invert = False
# Print the number of
# operations required
print (operations)
# Print the chosen prefix
# length in each operation
if (operations != 0):
for x in ops:
print(x, end = " ")
# Driver Code
if __name__ == '__main__':
# Given binary strings
A, B = "001", "000"
N = len(A)
findOperations(A, B, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count flips required
// to make Strings A and B equal
static void findOperations(String A,
String B, int N)
{
// Stores the count of the
// number of operations
int operations = 0;
// Stores the length of
// the chosen prefixes
List ops = new List();
// Stores if operations are
// performed even or odd times
bool invert = false;
// Traverse the given String
for(int i = N - 1; i >= 0; i--)
{
// If current characters in
// the two Strings are unequal
if (A[i] != B[i])
{
// If A[i] is not flipped
if (!invert)
{
// Increment count
// of operations
operations++;
// Insert the length of
// the chosen prefix
ops.Add(i + 1);
// Update invert to true
invert = true;
}
}
else
{
// If A[i] is flipped
if (invert)
{
// Increment count
// of operations
operations++;
// Insert length of
// the chosen prefix
ops.Add(i + 1);
// Update invert to false
invert = false;
}
}
}
// Print the number of
// operations required
Console.Write(operations + "\n");
// Print the chosen prefix
// length in each operation
if (operations != 0)
{
foreach(int x in ops)
Console.Write(x + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Given binary Strings
String A = "001", B = "000";
int N = A.Length;
findOperations(A, B, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
2
3 2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live