📜  二进制字符串中最长连续交换的长度最多交换一次

📅  最后修改于: 2021-06-26 01:56:24             🧑  作者: Mango

给定一个二进制字符串的长度N 。最多可以在0和1之间进行一次交换。任务是找到可以实现的最长连续1的长度。

例子:

Input : str = "111011101"
Output : 7 
We can swap 0 at 4th with 1 at 10th position

Input : str = "111000"
Output : 3 
We cannot obtain more than 3 1's after 

方法

  1. 在变量cnt_one中计数数组中的所有1。
  2. 维护两个向量或数组,这些向量或数组从左到右存储累积的值。
  3. 每当为0时:
    • 如果( left [i-1] + right [i + 1] )存储max_count = left [i-1] + right [i + 1] +1 ,因为通过交换,我们将得到一个额外的1代替0 。
    • 否则max_count = left [i-1] + right [i + 1]
  4. 输出是可以达到的max_count的最大值。

下面是上述方法的实现:

C++
// C++ program to find length of longest consecutive
// ones by at most one swap in a Binary String
  
#include 
using namespace std;
  
// Function to calculate the length of the
// longest consecutive 1's
int maximum_one(string s, int n)
{
    // To count all 1's in the string
    int cnt_one = 0;
      
    int max_cnt = 0, temp=0;
  
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            cnt_one++;
            temp++;
        }
        else
        {
            max_cnt = max(temp,max_cnt);
            temp=0;
        }
    }
      
    max_cnt = max(max_cnt, temp);
  
    // To store cumulative 1's
    int left[n], right[n];
  
    if (s[0] == '1')
        left[0] = 1;
    else
        left[0] = 0;
  
    if (s[n - 1] == '1')
        right[n - 1] = 1;
    else
        right[n - 1] = 0;
  
    // Counting cumulative 1's from left
    for (int i = 1; i < n; i++) {
        if (s[i] == '1')
            left[i] = left[i - 1] + 1;
  
        // If 0 then start new cumulative
        // one from that i
        else
            left[i] = 0;
    }
  
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] == '1')
            right[i] = right[i + 1] + 1;
  
        else
            right[i] = 0;
    }
  
  
    for (int i = 1; i < n - 1; i++) {
        // perform step 3 of the approach
        if (s[i] == '0') {
  
            // step 3
            int sum = left[i - 1] + right[i + 1];
  
            if (sum < cnt_one)
                max_cnt = max(max_cnt, sum+1);
  
            else
                max_cnt = max(max_cnt, sum);
  
        }
    }
  
    return max_cnt;
}
  
// Driver Code
int main()
{
    // string
    string s = "111011101";
  
    cout << maximum_one(s, s.length());
  
    return 0;
}


Java
// Java  program to find length of longest consecutive
// ones by at most one swap in a Binary String
  
import java.io.*;
  
class GFG {
   
// Function to calculate the length of the
// longest consecutive 1's
 static int maximum_one(String s, int n)
{
    // To count all 1's in the string
    int cnt_one = 0;
  
    int max_cnt = 0, temp=0;
  
    for (int i = 0; i < n; i++) {
        if (s.charAt(i) == '1')
        {
            cnt_one++;
            temp++;
        }
        else
        {
            max_cnt = Math.max(max_cnt, temp);
            temp = 0;
        }
    }
    max_cnt = Math.max(max_cnt, temp);
  
    // To store cumulative 1's
    int []left = new int[n];
     int right[]= new int[n];
  
    if (s.charAt(0) == '1')
        left[0] = 1;
    else
        left[0] = 0;
  
    if (s.charAt(n - 1) == '1')
        right[n - 1] = 1;
    else
        right[n - 1] = 0;
  
    // Counting cumulative 1's from left
    for (int i = 1; i < n; i++) {
        if (s.charAt(i) == '1')
            left[i] = left[i - 1] + 1;
  
        // If 0 then start new cumulative
        // one from that i
        else
            left[i] = 0;
    }
  
    for (int i = n - 2; i >= 0; i--) {
        if (s.charAt(i) == '1')
            right[i] = right[i + 1] + 1;
  
        else
            right[i] = 0;
    }
  
  
    for (int i = 1; i < n - 1; i++) {
        // perform step 3 of the approach
        if (s.charAt(i) == '0') {
  
            // step 3
            int sum = left[i - 1] + right[i + 1];
  
            if (sum < cnt_one)
                 max_cnt = Math.max(max_cnt, sum+1);
  
            else
                 max_cnt = Math.max(max_cnt, sum);
        }
    }
  
    return max_cnt;
}
  
// Driver Code
  
    public static void main (String[] args) {
        // string
    String s = "111011101";
  
    System.out.println( maximum_one(s, s.length()));
    }
}
// This code is contributed by anuj_67..


Python3
# Python 3 program to find length of 
# longest consecutive ones by at most
# one swap in a Binary String
  
# Function to calculate the length 
# of the longest consecutive 1's
def maximum_one(s, n) :
  
    # To count all 1's in the string
    cnt_one = 0
      
    cnt, max_cnt = 0, 0
  
    for i in range(n) :
        if (s[i] == '1') :
            cnt_one += 1
            cnt += 1
        else :
            max_cnt = max(max_cnt, cnt)
            cnt = 0
              
    max_cnt = max(max_cnt, cnt)
      
    # To store cumulative 1's
    left = [0] * n
    right = [0] * n
  
    if (s[0] == '1') :
        left[0] = 1
          
    else :
        left[0] = 0
  
    if (s[n - 1] == '1') :
        right[n - 1] = 1
          
    else :
        right[n - 1] = 0
  
    # Counting cumulative 1's from left
    for i in range(1, n) :
        if (s[i] == '1') :
            left[i] = left[i - 1] + 1
  
        # If 0 then start new cumulative
        # one from that i
        else :
            left[i] = 0
      
    for i in range(n - 2, -1, -1) :
          
        if (s[i] == '1') :
            right[i] = right[i + 1] + 1
  
        else :
            right[i] = 0
  
  
    for i in range(1, n) :
          
        # perform step 3 of the approach
        if (s[i] == '0') :
  
            # step 3
            sum = left[i - 1] + right[i + 1]
  
            if (sum < cnt_one) :
                max_cnt = max(max_cnt, sum+1)
  
            else :
                max_cnt = max(max_cnt, sum)
  
  
    return max_cnt
  
# Driver Code
if __name__ == "__main__" :
      
    # string
    s = "111011101"
  
    print(maximum_one(s, len(s)))
  
# This code is contributed by Ryuga


C#
// C# program to find length of longest consecutive
// ones by at most one swap in a Binary String
using System;
  
class GFG {
  
// Function to calculate the length of the
// longest consecutive 1's
static int maximum_one(string s, int n)
{
    // To count all 1's in the string
    int cnt_one = 0;
  
    for (int i = 0; i < n; i++) {
        if (s[i] == '1')
            cnt_one++;
    }
  
    // To store cumulative 1's
    int []left = new int[n];
    int []right= new int[n];
  
    if (s[0] == '1')
        left[0] = 1;
    else
        left[0] = 0;
  
    if (s[n - 1]== '1')
        right[n - 1] = 1;
    else
        right[n - 1] = 0;
  
    // Counting cumulative 1's from left
    for (int i = 1; i < n; i++) {
        if (s[i] == '1')
            left[i] = left[i - 1] + 1;
  
        // If 0 then start new cumulative
        // one from that i
        else
            left[i] = 0;
    }
  
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] == '1')
            right[i] = right[i + 1] + 1;
  
        else
            right[i] = 0;
    }
  
    int cnt = 0, max_cnt = 0;
  
    for (int i = 1; i < n - 1; i++) {
        // perform step 3 of the approach
        if (s[i] == '0') {
  
            // step 3
            int sum = left[i - 1] + right[i + 1];
  
            if (sum < cnt_one)
                cnt = sum + 1;
  
            else
                cnt = sum;
  
            max_cnt = Math.Max(max_cnt, cnt);
            cnt = 0;
        }
    }
  
    return max_cnt;
}
  
// Driver Code
  
    public static void Main () {
        // string
    string s = "111011101";
  
    Console.WriteLine( maximum_one(s, s.Length));
    }
}
// This code is contributed by inder_verma..


PHP
= 0; $i--)
    {
        if ($s[$i] == '1')
            $right[$i] = $right[$i + 1] + 1;
  
        else
            $right[$i] = 0;
    }
  
    $cnt = 0; $max_cnt = 0;
  
    for ($i = 1; $i < $n - 1; $i++) 
    {
        // perform step 3 of the approach
        if ($s[$i] == '0')
        {
  
            // step 3
            $sum = $left[$i - 1] + $right[$i + 1];
  
            if ($sum < $cnt_one)
                $cnt = $sum + 1;
  
            else
                $cnt = $sum;
  
            $max_cnt = max($max_cnt, $cnt);
            $cnt = 0;
        }
    }
    return $max_cnt;
}
  
// Driver Code
  
// string
$s = "111011101";
  
echo maximum_one($s, strlen($s));
  
// This code is contributed 
// by Akanksha Rai
?>


输出:
7

时间复杂度:O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。