📜  将子字符串“01”替换为“110”以完全删除它的最小替换次数

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

将子字符串“01”替换为“110”以完全删除它的最小替换次数

给定一个二进制字符串S ,任务是找到将子字符串“01”重复替换为字符串“110”的最小次数,使得给定字符串S中不存在任何子字符串“01”

例子:

方法:给定的问题可以使用贪心方法来解决。该操作是每当找到子字符串“01”时,将其替换为“110” ,现在“0”右侧出现“1”的数量形成子字符串“01” ,该子字符串参与将字符串更改为“110” 。因此,想法是从末尾遍历字符串,每当出现“0”时,执行给定的操作,直到右侧出现1的数量。请按照以下步骤解决问题:

  • 初始化两个变量,比如anscntOne都为0 ,以存储执行的最小操作数和遍历期间从末尾开始连续1的计数。
  • 从末尾遍历给定的字符串S并执行以下步骤:
    • 如果当前字符为0 ,则将ans增加到目前为止获得的连续1的数量和连续1的计数值的两倍。
    • 否则,将cntOne的值增加1
  • 完成上述步骤后,打印ans的值作为最小操作次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
 
using namespace std;
 
// Function to find the minimum number
// of replacement of "01" with "110"
// s.t. S doesn't contain substring "10"
void minimumOperations(string S, int N)
{
    // Stores the number of operations
    // performed
    int ans = 0;
 
    // Stores the resultant count
    // of substrings
    int cntOne = 0;
 
    // Traverse the string S from end
    for (int i = N - 1; i >= 0; i--) {
 
        // If the current character
        // is 0
        if (S[i] == '0') {
            ans += cntOne;
            cntOne *= 2;
        }
 
        // If the current character
        // is 1
        else
            cntOne++;
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    string S = "001";
    int N = S.length();
    minimumOperations(S, N);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
    // Function to find the minimum number
    // of replacement of "01" with "110"
    // s.t. S doesn't contain substring "10"
    public static void minimumOperations(String S, int N)
    {
        // Stores the number of operations
        // performed
        int ans = 0;
 
        // Stores the resultant count
        // of substrings
        int cntOne = 0;
 
        // Traverse the string S from end
        for (int i = N - 1; i >= 0; i--) {
 
            // If the current character
            // is 0
            if (S.charAt(i) == '0') {
                ans += cntOne;
                cntOne *= 2;
            }
 
            // If the current character
            // is 1
            else
                cntOne++;
        }
 
        // Print the result
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "001";
        int N = S.length();
        minimumOperations(S, N);
    }
}
 
  // This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of replacement of "01" with "110"
# s.t. S doesn't contain substring "10"
def minimumOperations(S, N):
     
    # Stores the number of operations
    # performed
    ans = 0
 
    # Stores the resultant count
    # of substrings
    cntOne = 0
 
    # Traverse the string S from end
    i = N - 1
     
    while(i >= 0):
         
        # If the current character
        # is 0
        if (S[i] == '0'):
            ans += cntOne
            cntOne *= 2
 
        # If the current character
        # is 1
        else:
            cntOne += 1
             
        i -= 1
 
    # Print the result
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    S = "001"
    N = len(S)
     
    minimumOperations(S, N)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum number
// of replacement of "01" with "110"
// s.t. S doesn't contain substring "10"
public static void minimumOperations(string S, int N)
{
     
    // Stores the number of operations
    // performed
    int ans = 0;
 
    // Stores the resultant count
    // of substrings
    int cntOne = 0;
 
    // Traverse the string S from end
    for(int i = N - 1; i >= 0; i--)
    {
         
        // If the current character
        // is 0
        if (S[i] == '0')
        {
            ans += cntOne;
            cntOne *= 2;
        }
 
        // If the current character
        // is 1
        else
            cntOne++;
    }
 
    // Print the result
    Console.WriteLine(ans);
}
 
// Driver code
static void Main()
{
    string S = "001";
    int N = S.Length;
     
    minimumOperations(S, N);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
3

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