将子字符串“01”替换为“110”以完全删除它的最小替换次数
给定一个二进制字符串S ,任务是找到将子字符串“01”重复替换为字符串“110”的最小次数,使得给定字符串S中不存在任何子字符串“01” 。
例子:
Input: S = “01”
Output: 1
Explanation:
Below are the operations performed:
Operation 1: Choosing substring (0, 1) in the string “01” and replacing it with “110” modifies the given string to “110”.
After the above operations, the string S(= “110”) doesn’t contain any substring as “01”. Therefore, the total number of operation required is 1.
Input: S = “001”
Output:3
Explanation:
Below are the operations performed:
Operation 1: Choosing substring (1, 2) in the string “001” and replacing it with “110” modifies the given string to “0110”.
Operation 2: Choosing substring (0, 1) in the string “0110” and replacing it with “110” modifies the given string to “11010”.
Operation 3: Choosing substring (2, 3) in the string “11010” and replacing it with “110” modifies the given string to “111100”.
After the above operations, the string S(= “111100”) doesn’t contain any substring as “01”. Therefore, the total number of operation required is 3.
方法:给定的问题可以使用贪心方法来解决。该操作是每当找到子字符串“01”时,将其替换为“110” ,现在“0”右侧出现“1”的数量形成子字符串“01” ,该子字符串参与将字符串更改为“110” 。因此,想法是从末尾遍历字符串,每当出现“0”时,执行给定的操作,直到右侧出现1的数量。请按照以下步骤解决问题:
- 初始化两个变量,比如ans和cntOne都为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)