给定长度为N的二进制字符串S ,任务是计算为使字符串S交替需要反转的S的最小数目的子字符串。如果不可能使字符串交替出现,则打印“ -1” 。
例子:
Input: S = “10001110”
Output: 2
Explanation:
In the first operation, reversing the substring {S[3], .., S[6]} modifies the string to “10110010”.
In the second operation, reversing the substring {S[4], .. S[5]}modifies the string to “10101010”, which is alternating.
Input: S = “100001”
Output: -1
Explanation: Not possible to obtain an alternating binary string.
方法:该想法基于以下观察结果:当子字符串s [L,R]反转时,则不超过两对s [L – 1] , s [L]和s [R] , S [R + 1 ]被更改。而且,一对应该是连续的00对,而另一对应该是11 。因此,可以通过将00与11或与S的左/右边界配对来获得最少的操作数。因此,所需的操作次数是相同字符的连续对的数目的一半。请按照以下步骤解决问题:
- 遍历字符串S以计算1和0的数目,并将它们分别存储在sum1和sum0中。
- 如果sum1和sum0的绝对差> 1 ,则打印“ -1” 。
- 否则,找到字符串S中相同的连续字符数。设该计数为K表示1,L表示0 。
- 完成上述步骤后,打印K值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of substrings required to be reversed
// to make the string S alternating
int minimumReverse(string s, int n)
{
// Store count of consecutive pairs
int k = 0 , l = 0 ;
// Stores the count of 1s and 0s
int sum1 = 0, sum0 = 0;
// Traverse through the string
for (int i = 1; i < n; i++) {
if (s[i] == '1')
// Increment 1s count
sum1++;
else
// Increment 0s count
sum0++;
// Increment K if consecutive
// same elements are found
if (s[i] == s[i - 1]&& s[i] == '0')
k++;
else if( s[i] == s[i - 1]&& s[i] == '1')
l++;
}
// Increment 1s count
if(s[0]=='1')
sum1++;
else // Increment 0s count
sum0++;
// Check if it is possible or not
if (abs(sum1 - sum0) > 1)
return -1;
// Otherwise, print the number
// of required operations
return max(k , l );
}
// Driver Code
int main()
{
string S = "10001";
int N = S.size();
// Function Call
cout << minimumReverse(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG
{
// Function to count the minimum number
// of substrings required to be reversed
// to make the string S alternating
static int minimumReverse(String s, int n)
{
// Store count of consecutive pairs
int k = 0 , l = 0 ;
// Stores the count of 1s and 0s
int sum1 = 0, sum0 = 0;
// Traverse through the string
for (int i = 1; i < n; i++)
{
if (s.charAt(i) == '1')
// Increment 1s count
sum1++;
else
// Increment 0s count
sum0++;
// Increment K if consecutive
// same elements are found
if (s.charAt(i) == s.charAt(i - 1) && s.charAt(i) == '0')
k++;
else if( s.charAt(i) == s.charAt(i - 1) && s.charAt(i) == '1')
l++;
}
// Increment 1s count
if(s.charAt(0)=='1')
sum1++;
else // Increment 0s count
sum0++;
// Check if it is possible or not
if (Math.abs(sum1 - sum0) > 1)
return -1;
// Otherwise, print the number
// of required operations
return Math.max(k , l);
}
// Driver code
public static void main (String[] args)
{
String S = "10001";
int N = S.length();
// Function Call
System.out.print(minimumReverse(S, N));
}
}
// This code is contributed by offbeat
Python3
# Python program for the above approach
# Function to count the minimum number
# of substrings required to be reversed
# to make the string S alternating
def minimumReverse(s, n):
# Store count of consecutive pairs
k = 0;
l = 0;
# Stores the count of 1s and 0s
sum1 = 0;
sum0 = 0;
# Traverse through the string
for i in range(1, n):
if (s[i] == '1'):
# Increment 1s count
sum1 += 1;
else:
# Increment 0s count
sum0 += 1;
# Increment K if consecutive
# same elements are found
if (s[i] == s[i - 1] and s[i] == '0'):
k += 1;
elif (s[i] == s[i - 1] and s[i] == '1'):
l += 1;
# Increment 1s count
if (s[0] == '1'):
sum1 += 1;
else: # Increment 0s count
sum0 += 1;
# Check if it is possible or not
if (abs(sum1 - sum0) > 1):
return -1;
# Otherwise, prthe number
# of required operations
return max(k, l);
# Driver code
if __name__ == '__main__':
S = "10001";
N = len(S);
# Function Call
print(minimumReverse(S, N));
# This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count the minimum number
// of substrings required to be reversed
// to make the string S alternating
static int minimumReverse(String s, int n)
{
// Store count of consecutive pairs
int k = 0 , l = 0 ;
// Stores the count of 1s and 0s
int sum1 = 0, sum0 = 0;
// Traverse through the string
for (int i = 1; i < n; i++)
{
if (s[i] == '1')
// Increment 1s count
sum1++;
else
// Increment 0s count
sum0++;
// Increment K if consecutive
// same elements are found
if (s[i] == s[i-1] && s[i] == '0')
k++;
else if( s[i] == s[i-1] && s[i] == '1')
l++;
}
// Increment 1s count
if(s[0] == '1')
sum1++;
else // Increment 0s count
sum0++;
// Check if it is possible or not
if (Math.Abs(sum1 - sum0) > 1)
return -1;
// Otherwise, print the number
// of required operations
return Math.Max(k , l);
}
// Driver code
public static void Main(String[] args)
{
String S = "10001";
int N = S.Length;
// Function Call
Console.Write(minimumReverse(S, N));
}
}
// This code is contributed by shikhasingrajput
2
时间复杂度: O(N)
辅助空间: O(1)