📌  相关文章
📜  在给定的二进制字符串中将所有 1 组合在一起所需的 1 子串的最小移位

📅  最后修改于: 2021-09-03 13:49:04             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是打印最小数量的索引,只包含1 的子字符串需要移位,以便字符串中存在的所有1都组合在一起。

例子:

方法:该问题可以通过观察所需的最小操作数等于任何一对连续1之间存在的0数来解决。请按照以下步骤解决问题:

  1. 遍历字符串的字符和搜索字符“1”的第一个和最后一个事件,并将其存储在变量,分别说firstOnelastOne。
  2. 遍历范围[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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live