使给定的二进制字符串相等所需的翻转相邻位的最小数量
给定两个长度为N的二进制字符串s1[]和s2[] ,任务是找到使它们相等的最小操作数。如果不可能,则打印-1 。一种操作被定义为选择二进制字符串之一的两个相邻索引并将这些位置处的字符反转,即从1到0 ,反之亦然。
例子:
Input: s1[] = “0101”, s2[] = “1111”
Output: 2
Explanation: Invert the characters at 1 and 2 indices in the first string and at 0 and 1 indices in the second string to make them equal as 0011. There are other ways to do also like converting to 1111, etc.
Input: s1[] = “011”, s2[] = “111”
Output: -1
方法:想法是线性遍历两个字符串,如果在任何索引处字符不同,则反转字符串s1[]中的第i个和第(i+1)个字符。请按照以下步骤解决问题:
- 将变量count初始化为0以存储答案。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 如果s1[i]不等于s2[i] ,则执行以下任务:
- 如果s1[i]等于1,则将其更改为0 ,否则将其更改为1。
- 同样,如果s1[i+1]等于1,则将其更改为0 ,否则将其更改为1。
- 最后,将count的值增加1。
- 如果s1[i]不等于s2[i] ,则执行以下任务:
- 如果s1[]等于s2[],则返回count的值作为答案,否则返回-1。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of inversions required.
int find_Min_Inversion(int n, string s1, string s2)
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (s1 == s2) {
return count;
}
return -1;
}
// Driver Code
int main()
{
int n = 4;
string s1 = "0101";
string s2 = "1111";
cout << find_Min_Inversion(n, s1, s2) << endl;
return 0;
}
C
// C program for the above approach
#include
#include
// Function to find the minimum number
// of inversions required.
int find_Min_Inversion(int n, char s1[1000], char s2[1000])
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (strcmp(s1, s2) != -1) {
return count;
}
return -1;
}
// Driver Code
int main()
{
int n = 4;
char s1[1000] = "0101";
char s2[1000] = "1111";
printf("%d\n", find_Min_Inversion(n, s1, s2));
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the minimum number
// of inversions required.
static int find_Min_Inversion(int n, char[] s1,
char[] s2)
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (String.copyValueOf(s1).equals(
String.copyValueOf(s2))) {
return count;
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
String s1 = "0101";
String s2 = "1111";
System.out.print(
find_Min_Inversion(n, s1.toCharArray(),
s2.toCharArray())
+ "\n");
}
}
// This code is contributed by umadevi9616
Python3
# Python 3 program for the above approach
# Function to find the minimum number
# of inversions required.
def find_Min_Inversion(n, s1, s2):
# Initializing the answer
count = 0
# Iterate over the range
s1 = list(s1)
s2 = list(s2)
for i in range(n - 1):
if (s1[i] != s2[i]):
# If s1[i]!=s2[i], then inverse
# the characters at i snd (i+1)
# positions in s1.
if (s1[i] == '1'):
s1[i] = '0'
else:
s1[i] = '1'
if (s1[i + 1] == '1'):
s1[i + 1] = '0'
else:
s1[i + 1] = '1'
# Adding 1 to counter
# if characters are not same
count += 1
s1 = ''.join(s1)
s2 = ''.join(s2)
if (s1 == s2):
return count
return -1
# Driver Code
if __name__ == '__main__':
n = 4
s1 = "0101"
s2 = "1111"
print(find_Min_Inversion(n, s1, s2))
# This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)