给定一个二进制字符串S和一个子字符串K ,任务是找到翻转二进制字符串的字符以使其不包含给定子字符串K所需的最少步骤数。注意:第一步,我们可以将0更改为1,反之亦然。
例子:
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.
天真的方法:天真的方法是使用模式搜索。遍历字符串N!次(N =二进制字符串的长度)。在每次迭代中,检查子字符串K ,如果匹配,则增加计数。最后,打印计数,该计数将是使字符串不包含给定子字符串K所需的步骤数。
时间复杂度: O(M * N)其中M是子字符串的长度, N是二进制字符串的长度。
辅助空间: O(1)
高效方法:为了优化上述方法,这个想法是用空字符串来代替子串和减去原始字符串长度所得到的字符串长度。之后,将所得字符串与给定子字符串K的长度相除,以得到翻转给定字符串S的字符所需的最少步骤数,从而不包含给定子字符串K。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
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
1
时间复杂度: O(N) ,其中N是字符串的长度。
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。