📜  最多翻转 K 个字符后连续 1 或 0 的最大长度

📅  最后修改于: 2022-05-13 01:56:08.774000             🧑  作者: Mango

最多翻转 K 个字符后连续 1 或 0 的最大长度

给定一个大小为N的二进制字符串S和一个整数K ,任务是在翻转给定二进制字符串S最多 K个字符后找到连续10的最大长度。

例子

方法:给定的问题可以使用两指针方法和滑动窗口方法来解决。请按照以下步骤解决给定问题:

  • 初始化一个函数,比如maxLength(S, N, ch, K) ,它使用以下步骤在翻转最多 K个字符后找到字符ch的最大长度:
    1. 初始化一个变量,比如cnt ,它存储窗口中字符ch的计数。
    2. 初始化一个变量,比如left ,它存储了结果窗口的开始。
    3. 初始化一个变量,比如ans ,它在最多 K个翻转后将连续K个字符的结果长度存储为ch
    4. 使用变量right遍历字符串S并执行以下步骤:
      • 如果S[right]的值为ch ,则将cnt的值增加1
      • 迭代直到cnt的值大于K ,然后递增左指针并将cnt的值递减1
      • ans的值更新为ans(right – left + 1)的最大值。
  • 完成上述步骤后,打印maxLength(S, N, '0', K)maxLength(S, N, '1', K)返回的值的最大值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum length
// continuous segment of character c after
// flipping at most K characters
int maxLength(string str, int n,
              char c, int k)
{
    // Stores the maximum length
    int ans = -1;
 
    // Stores the count of char 'c'
    int cnt = 0;
 
    // Start of window
    int left = 0;
 
    for (int right = 0; right < n; right++) {
 
        if (str[right] == c) {
            cnt++;
        }
 
        // Remove the extra 'c' from left
        while (cnt > k) {
            if (str[left] == c) {
                cnt--;
            }
 
            // Increment the value of
            // the left
            left++;
        }
 
        // Update the resultant maximum
        // length of character ch
        ans = max(ans, right - left + 1);
    }
 
    return ans;
}
 
// Function to find the maximum length
// of consecutive 0s or 1s by flipping
// at most K characters of the string
int maxConsecutiveSegment(string S, int K)
{
    int N = S.length();
 
    // Print the maximum of the maximum
    // length of 0s or 1s
    return max(maxLength(S, N, '0', K),
               maxLength(S, N, '1', K));
}
 
// Driver Code
int main()
{
    string S = "1001";
    int K = 1;
    cout << maxConsecutiveSegment(S, K);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG {
     
    // Function to find the maximum length
    // continuous segment of character c after
    // flipping at most K characters
    static int maxLength(String str, int n,
                  char c, int k)
    {
       
        // Stores the maximum length
        int ans = -1;
     
        // Stores the count of char 'c'
        int cnt = 0;
     
        // Start of window
        int left = 0;
     
        for (int right = 0; right < n; right++) {
     
            if (str.charAt(right) == c) {
                cnt++;
            }
     
            // Remove the extra 'c' from left
            while (cnt > k) {
                if (str.charAt(left) == c) {
                    cnt--;
                }
     
                // Increment the value of
                // the left
                left++;
            }
     
            // Update the resultant maximum
            // length of character ch
            ans = Math.max(ans, right - left + 1);
        }
     
        return ans;
    }
     
    // Function to find the maximum length
    // of consecutive 0s or 1s by flipping
    // at most K characters of the string
    static int maxConsecutiveSegment(String S, int K)
    {
        int N = S.length();
     
        // Print the maximum of the maximum
        // length of 0s or 1s
        return Math.max(maxLength(S, N, '0', K),
                   maxLength(S, N, '1', K));
    }
     
    // Driver Code
    int main()
    {
 
     
        return 0;
    }
   
  // Driver code
    public static void main (String[] args) {
         
        String S = "1001";
        int K = 1;
        System.out.println(maxConsecutiveSegment(S, K));
         
    }
}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
 
# Function to find the maximum length
# continuous segment of character c after
# flipping at most K characters
def maxLength(str, n, c, k):
 
    # Stores the maximum length
    ans = -1
 
    # Stores the count of char 'c'
    cnt = 0
 
    # Start of window
    left = 0
 
    for right in range(0, n):
 
        if (str[right] == c):
            cnt += 1
 
        # Remove the extra 'c' from left
        while (cnt > k):
            if (str[left] == c):
                cnt -= 1
 
            # Increment the value of
            # the left
            left += 1
 
        # Update the resultant maximum
        # length of character ch
        ans = max(ans, right - left + 1)
 
    return ans
 
 
# Function to find the maximum length
# of consecutive 0s or 1s by flipping
# at most K characters of the string
def maxConsecutiveSegment(S,  K):
 
    N = len(S)
 
    # Print the maximum of the maximum
    # length of 0s or 1s
    return max(maxLength(S, N, '0', K), maxLength(S, N, '1', K))
 
 
# Driver Code
if __name__ == "__main__":
 
    S = "1001"
    K = 1
    print(maxConsecutiveSegment(S, K))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to find the maximum length
    // continuous segment of character c after
    // flipping at most K characters
    static int maxLength(String str, int n,
                  char c, int k)
    {
 
        // Stores the maximum length
        int ans = -1;
 
        // Stores the count of char 'c'
        int cnt = 0;
 
        // Start of window
        int left = 0;
 
        for (int right = 0; right < n; right++)
        {
 
            if (str[right] == c)
            {
                cnt++;
            }
 
            // Remove the extra 'c' from left
            while (cnt > k)
            {
                if (str[left] == c)
                {
                    cnt--;
                }
 
                // Increment the value of
                // the left
                left++;
            }
 
            // Update the resultant maximum
            // length of character ch
            ans = Math.Max(ans, right - left + 1);
        }
 
        return ans;
    }
 
    // Function to find the maximum length
    // of consecutive 0s or 1s by flipping
    // at most K characters of the string
    static int maxConsecutiveSegment(String S, int K)
    {
        int N = S.Length;
 
        // Print the maximum of the maximum
        // length of 0s or 1s
        return Math.Max(maxLength(S, N, '0', K),
                   maxLength(S, N, '1', K));
    }
 
 
    // Driver code
    public static void Main()
    {
 
        String S = "1001";
        int K = 1;
        Console.WriteLine(maxConsecutiveSegment(S, K));
 
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript



输出:
3

时间复杂度: O(N)
辅助空间: O(1)