Minimum number of steps needed to remove the substring K from given string
Last Updated :
01 Jun, 2021
Given a binary string S, and a substring K, the task is to find the minimum no of steps required to flip the characters in a binary string such that it doesn't contain the given substring K. Note: In one step we can change 0 to 1 or vice versa.
Examples:
Input: S = "0111011", K = "011"
Output: 2
Explanation:
In the above string we have the substring 011 for 2 times, hence change the 3rd bit and 7th bit.
Input: S = "010010", K = "0100"
Output: 1
Explanation:
In the above string we have the substring 0100 for 1 time, change the 4th bit of the string to 1.
Naive Approach: The naive approach is to use the Pattern Searching. Iterate through the string for N! times(N = length of the binary string). In every iteration check for the substring K and if its a match then increment the count. At last, print the count which will be the number of steps required to make the string such that it doesn't contain the given substring K.
Time Complexity: O(M*N) where M is the length of substring and N is the length of the binary string.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above method, the idea is to replace the substring with an empty string and subtract the resultant string length from original string length. After this divide the resultant string with the length of the given substring K to get the minimum no of steps required to flip the characters of the given string S so that it doesn't contain the given substring K.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include<bits/stdc++.h>
#include <boost/algorithm/string/replace.hpp>
using namespace std;
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
int flipCharacters(string b,
string sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.size();
boost::replace_all(b, sub, "");
res = res - b.size();
// Divide the result
// with substring length
int temp = res / sub.size();
// Return the final count
return temp;
}
// Driver Code
int main()
{
// Given string S and substring K
string S = "010010";
string K = "0100";
// Function call
int result = flipCharacters(S, K);
// Print the minimum flip
cout << (result);
}
// This code is contributed by Amit Katiyar
Java
// Java program for the above approach
import java.util.*;
public class Test {
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
static int flipCharacters(String b,
String sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.length()
- b.replaceAll(sub, "")
.length();
// Divide the result
// with substring length
int temp = res / sub.length();
// Return the final count
return temp;
}
// Driver Code
public static void main(String[] args)
{
// Given string S and substring K
String S = "010010";
String K = "0100";
// Function Call
int result = flipCharacters(S, K);
// Print the minimum flip
System.out.println(result);
}
}
Python3
# Python3 program for the above approach
def flipCharacters(b, sub):
# Replace the substring with
# "" (emptryString)
b1 = b.replace(sub, "")
n = int((len(b)-len(b1))/len(sub))
return n
# Driver Code
if __name__ == '__main__':
# Given string S and substring K
S ="010010"
X ="0100"
# Function Call
result = flipCharacters(S, X)
# Print the minimum flip
print(result)
C#
// C# program for the above approach
using System;
class GFG
{
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
static int flipCharacters(string b,
string sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.Length -
b.Replace(sub, "").Length;
// Divide the result
// with substring length
int temp = res / sub.Length;
// Return the final count
return temp;
}
// Driver Code
public static void Main(string[] args)
{
// Given string S and substring K
string S = "010010";
string K = "0100";
// Function Call
int result = flipCharacters(S, K);
// Print the minimum flip
Console.Write(result);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript program for the above approach
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
function flipCharacters(b, sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
var res = b.length - b.replace(sub, "").length;
// Divide the result
// with substring length
var temp = parseInt(res / sub.length);
// Return the final count
return temp;
}
// Driver Code
// Given string S and substring K
var S = "010010";
var K = "0100";
// Function call
var result = flipCharacters(S, K);
// Print the minimum flip
document.write(result);
</script>
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Similar Reads
Maximum number of removals of given subsequence from a string Given string str, the task is to count the maximum number of possible operations that can be performed on str. An operation consists of taking a sub-sequence 'gks' from the string and removing it from the string. Examples: Input: str = "ggkssk"Output: 1Explanation: After 1st operation: str = "gsk"No
6 min read
Maximum number of given operations to remove the entire string Given string str containing lowercase English characters, we can perform the following two operations on the given string: Remove the entire string.Remove a prefix of the string str[0...i] only if it is equal to the sub-string str[(i + 1)...(2 * i + 1)]. The task is to find the maximum number of ope
11 min read
Minimum steps to delete a string after repeated deletion of palindrome substrings Given a string str, containing only digits from '0' to '9'. Your task is to find the minimum number of operations required to delete all the digits of string, where in each operation we can delete a palindromic substring.Note: After deleting the substring, the remaining parts are concatenated.Exampl
15+ min read
Minimum steps to delete a string by deleting substring comprising of same characters Given string str. You are allowed to delete only some contiguous characters if all the characters are the same in a single operation. The task is to find the minimum number of operations required to completely delete the string. Examples: Input: str = "abcddcba" Output: 4 Explanation: Delete dd, the
7 min read
Get K-th letter of the decoded string formed by repeating substrings Given a string S containing letter and digit and an integer K where, 2\leq S.length() \leq 100 and 1\leq K \leq 10^{9} . The task is to return the K-th letter of the new string S'.The new string S' is formed from old string S by following steps: 1. If the character read is a letter, that letter is a
6 min read
Substring of length K having maximum frequency in the given string Given a string str, the task is to find the substring of length K which occurs the maximum number of times. If more than one string occurs maximum number of times, then print the lexicographically smallest substring. Examples: Input: str = "bbbbbaaaaabbabababa", K = 5Output: ababaExplanation:The sub
14 min read
Min Length String with All Substrings of Size N Given two integers n and k, your task is to find a string of minimum length to contain all possible strings of size n as a substring. The characters of the string should be integers ranging from 0 to k - 1. Examples:Input: n = 2, k = 2Output: 00110Explanation: Allowed characters are from 0 to k-1 (i
7 min read
Smallest substring occurring only once in a given string Given a string S consisting of N lowercase alphabets, the task is to find the length of the smallest substring in S whose occurrence is exactly 1. Examples: Input: S = "abaaba"Output: 2Explanation: The smallest substring in the string S, whose occurrence is exactly 1 is "aa" . Length of this substri
6 min read
Minimize length of a string by removing occurrences of another string from it as a substring Given a string S and a string T, the task is to find the minimum possible length to which the string S can be reduced to after removing all possible occurrences of string T as a substring in string S. Examples: Input: S = "aabcbcbd", T = "abc"Output: 2Explanation:Removing the substring {S[1], ..., S
7 min read
K-th lexicographically smallest unique substring of a given string Given a string S. The task is to print the K-th lexicographically the smallest one among the different substrings of s.A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z, and an empty stri
5 min read