给定两个大小分别为N和M 的二进制字符串S1和S2 ,任务是找到将字符串S1转换为S2所需的相同字符子串的最小反转次数。如果无法将字符串S1转换为S2 ,则打印“-1” 。
例子:
Input: S1 = “100001”, S2 = “110111”
Output: 2
Explanation:
Initially string S1 = “100001”.
Reversal 1: Reverse the substring S1[1, 1], then the string S1 becomes “110001”.
Reversal 2: Reverse the substring S1[3, 4], then the string S1 becomes “110111”.
After the above reversals, the string S1 and S2 are equal.
Therefore, the count of reversals is 2.
Input: S1 = 101, S2 = 10
Output: -1
方法:按照以下步骤解决此问题:
- 初始化一个变量,比如answer ,以存储所需的反转结果计数。
- 如果给定的字符串S1和S2的长度不同,则打印“-1” 。
- 迭代范围[0, N – 1]并执行以下步骤:
- 如果S1[i]和S2[i]不相同,则迭代直到S1[i]和S2[i]相同。由于需要翻转当前子字符串,因此将答案增加1 。
- 否则,继续下一次迭代。
- 完成上述步骤后,将答案的值打印为所需的子串翻转结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of reversals required to make the
// given binary strings s1 and s2 same
int canMakeSame(string s1, string s2)
{
// Stores the minimum count of
// reversal of substrings required
int ans = 0;
// If the length of the strings
// are not the same then return -1
if (s1.size() != s2.size()) {
return -1;
}
int N = s1.length();
// Iterate over each character
for (int i = 0; i < N; i++) {
// If s1[i] is not
// equal to s2[i]
if (s1[i] != s2[i]) {
// Iterate until s1[i] != s2[i]
while (i < s1.length()
&& s1[i] != s2[i]) {
i++;
}
// Increment answer by 1
ans++;
}
}
// Return the resultant count of
// reversal of substring required
return ans;
}
// Driver Code
int main()
{
string S1 = "100001";
string S2 = "110111";
// Function Call
cout << canMakeSame(S1, S2);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to count the minimum number
// of reversals required to make the
// given binary strings s1 and s2 same
static int canMakeSame(String s1, String s2)
{
// Stores the minimum count of
// reversal of substrings required
int ans = 0;
// If the length of the strings
// are not the same then return -1
if (s1.length() != s2.length()) {
return -1;
}
int N = s1.length();
// Iterate over each character
for (int i = 0; i < N; i++)
{
// If s1[i] is not
// equal to s2[i]
if (s1.charAt(i) != s2.charAt(i))
{
// Iterate until s1[i] != s2[i]
while (i < s1.length()
&& s1.charAt(i) != s2.charAt(i))
{
i++;
}
// Increment answer by 1
ans++;
}
}
// Return the resultant count of
// reversal of substring required
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S1 = "100001";
String S2 = "110111";
// Function Call
System.out.println(canMakeSame(S1, S2));
}
}
// This code is contributed by Dharanendra L V
Python3
# Python3 program for the above approach
# Function to count the minimum number
# of reversals required to make the
# given binary strings s1 and s2 same
def canMakeSame(s1, s2) :
# Stores the minimum count of
# reversal of substrings required
ans = -1
# If the length of the strings
# are not the same then return -1
if (len(s1) != len(s2)) :
return -1
N = len(s1)
# Iterate over each character
for i in range(0, N):
# If s1[i] is not
# equal to s2[i]
if (s1[i] != s2[i]) :
# Iterate until s1[i] != s2[i]
while (i < len(s1)
and s1[i] != s2[i]) :
i += 1
# Increment answer by 1
ans += 1
# Return the resultant count of
# reversal of subrequired
return ans
# Driver Code
S1 = "100001"
S2 = "110111"
# Function Call
print(canMakeSame(S1, S2))
# This code is contributed by code_hunt.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the minimum number
// of reversals required to make the
// given binary strings s1 and s2 same
static int canMakeSame(string s1, string s2)
{
// Stores the minimum count of
// reversal of substrings required
int ans = 0;
// If the length of the strings
// are not the same then return -1
if (s1.Length != s2.Length) {
return -1;
}
int N = s1.Length;
// Iterate over each character
for (int i = 0; i < N; i++)
{
// If s1[i] is not
// equal to s2[i]
if (s1[i] != s2[i])
{
// Iterate until s1[i] != s2[i]
while (i < s1.Length
&& s1[i] != s2[i])
{
i++;
}
// Increment answer by 1
ans++;
}
}
// Return the resultant count of
// reversal of substring required
return ans;
}
// Driver Code
public static void Main(string[] args)
{
string S1 = "100001";
string S2 = "110111";
// Function Call
Console.Write(canMakeSame(S1, S2));
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live