给定一个长度为N的二进制字符串S ,任务是打印最小数量的索引,只包含1 的子字符串需要移位,以便字符串中存在的所有1都组合在一起。
例子:
Input: S = “00110111011”
Output: 2
Explanation:
Operation 1: Shift substring {S[2], S[3]} (0-based indexing) to right and place between S[4] and S[5]. Now, S is modified to “00011111011”.
Operation 2: Shift substring {S[10], S[11]} to left and place between S[7] and S[8]. Now, S is modified to “00011111110”.
Therefore, 2 substrings are required to be shifted.
Input: S = “1001001”
Output: 4
Explanation:
Operation 1: Shift ‘1’ at S[0] to right by two indices and place between S[2] and S[3]. Now, S is modified to “0011001”.
Operation 2: Shift ‘1’ at S[6] to left by two indices and place between S[3] and S[4]. Now, S is modified to “0011100”.
Therefore, 4 substrings are required to be shifted.
方法:该问题可以通过观察所需的最小操作数等于任何一对连续1之间存在的0数来解决。请按照以下步骤解决问题:
- 迭代字符串的字符和搜索字符“1”的第一个和最后一个事件,并将其存储在变量,分别说firstOne和lastOne。
- 遍历范围[firstOne, lastOne]并计算子字符串{S[firstOne], .. , S[lastOne]} 中存在的‘0’的数量,并将其打印为所需的输出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count indices substrings
// of 1s need to be shifted such that
// all 1s in the string are grouped together
void countShifts(string str)
{
// Stores first occurrence of '1'
int firstOne = -1;
// Stores last occurrence of '1'
int lastOne = -1;
// Count of 0s between firstOne and lastOne
int count = 0;
// Traverse the string to find the
// first and last occurrences of '1'
for (int i = 0; i < str.length(); i++) {
if (str[i] == '1') {
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne)) {
cout << 0;
return;
}
// Count number of 0s present between
// firstOne and lastOne
for (int i = firstOne; i <= lastOne; i++) {
if (str[i] == '0') {
count++;
}
}
// Print minimum operations
cout << count << endl;
}
// Driver Code
int main()
{
// Given string
string str = "00110111011";
countShifts(str);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to count indices substrings
// of 1s need to be shifted such that
// all 1s in the string are grouped together
static void countShifts(String str)
{
// Stores first occurrence of '1'
int firstOne = -1;
// Stores last occurrence of '1'
int lastOne = -1;
// Count of 0s between firstOne and lastOne
int count = 0;
// Traverse the string to find the
// first and last occurrences of '1'
for(int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == '1')
{
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne))
{
System.out.print(0);
return;
}
// Count number of 0s present between
// firstOne and lastOne
for(int i = firstOne; i <= lastOne; i++)
{
if (str.charAt(i) == '0')
{
count++;
}
}
// Print minimum operations
System.out.println(count);
}
// Driver code
public static void main (String[] args)
{
// Given string
String str = "00110111011";
countShifts(str);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function to count indices substrings
# of 1s need to be shifted such that
# all 1s in the string are grouped
# together
def countShifts(str):
# Stores first occurrence of '1'
firstOne = -1
# Stores last occurrence of '1'
lastOne = -1
# Count of 0s between firstOne
# and lastOne
count = 0
# Traverse the string to find the
# first and last occurrences of '1'
for i in range(len(str)):
if (str[i] == '1'):
if (firstOne == -1):
firstOne = i
lastOne = i
if ((firstOne == -1) or
(firstOne == lastOne)):
print(0)
return
# Count number of 0s present between
# firstOne and lastOne
for i in range(firstOne, lastOne + 1, 1):
if (str[i] == '0'):
count += 1
# Print minimum operations
print(count)
# Driver Code
# Given string
str = "00110111011"
countShifts(str)
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to count indices substrings
// of 1s need to be shifted such that
// all 1s in the string are grouped together
static void countShifts(string str)
{
// Stores first occurrence of '1'
int firstOne = -1;
// Stores last occurrence of '1'
int lastOne = -1;
// Count of 0s between firstOne and lastOne
int count = 0;
// Traverse the string to find the
// first and last occurrences of '1'
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '1')
{
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne))
{
Console.Write(0);
return;
}
// Count number of 0s present between
// firstOne and lastOne
for (int i = firstOne; i <= lastOne; i++)
{
if (str[i] == '0')
{
count++;
}
}
// Print minimum operations
Console.WriteLine(count);
}
// Driver code
static void Main()
{
// Given string
string str = "00110111011";
countShifts(str);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。