如果 S[i] 为 1,则通过将范围 [i+1, i+K] 中的所有 0 更改为 1,将给定的二进制字符串S 转换为全 1
给定一个大小为N的二进制字符串S和一个数字K ,任务是找出是否所有的“0”都可以在任意数量的操作中变成“ 1” 。在一个操作中,如果S[i]最初是'1' ,那么[i+1, i+K]范围内的所有'0都可以更改为'1' ,因为0≤i
例子:
Input: S=”100100″, N=6, K=2
Output: YES
Explanation: S[0] can be used to change S[1] and S[2] into 1s
S[3] can be used to change S[4] and S[5] into 1s
Input: S=”110000″, N=6, K=2
Output: NO
朴素方法:最简单的方法是以相反的方式遍历字符串,如果遇到“0” ,则检查左侧最近的“1”的位置是否超过K个索引。如果为真,则打印“NO” 。
时间复杂度: O(N*K)
辅助空间: O(1)
高效方法:为了优化上述方法,想法是使用堆栈。请按照以下步骤解决问题:
- 初始化两个变量flag并分别计数为1和0以存储结果并计算最后一次出现 ' 1'改变的 ' 0'的数量。
- 初始化一个空堆栈st 。
- 遍历字符串S ,然后执行以下操作:
- 如果堆栈为空:
- 如果当前元素是'0' ,将flag更改为0并中断,因为这个 ' 0'不能更改为'1' 。
- 否则,将count更新为0并将1推送到st 。
- 否则:
- 如果当前元素为“0”,请执行以下操作:
- 将计数增加 1。
- 如果count等于K ,则弹出堆栈st并将count更新为0
- 否则,将 count更新为0 。
- 如果当前元素为“0”,请执行以下操作:
- 如果堆栈为空:
- 如果flag的值为1 ,则打印“YES” ,否则打印“NO”作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether all 0s
// in the string can be changed into 1s
void changeCharacters(string S, int N, int K)
{
int flag = 1;
// Store the count of 0s converted
// for the last occurrence of 1
int count = 0;
// Declere a stack
stack st;
// Traverse the string, S
for (int i = 0; i < N; i++) {
// If stack is empty
if (st.empty()) {
// There is no 1 that can
// change this 0 to 1
if (S[i] == '0') {
flag = 0;
break;
}
// Push 1 into the stack
count = 0;
st.push(S[i]);
}
else {
if (S[i] == '0') {
count++;
// The last 1 has reached
// its limit
if (count == K) {
st.pop();
count = 0;
}
}
// New 1 has been found which
// can now change at most K 0s
else {
count = 0;
}
}
}
// If flag is 1, print "YES"
// else print "NO"
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
// Driver code
int main()
{
// Given Input
string S = "100100";
int N = S.length();
int K = 2;
// Function call
changeCharacters(S, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check whether all 0s
// in the string can be changed into 1s
static void changeCharacters(String S, int N, int K)
{
int flag = 1;
// Store the count of 0s converted
// for the last occurrence of 1
int count = 0;
// Declere a stack
Stack st = new Stack<>();
// Traverse the string, S
for(int i = 0; i < N; i++)
{
// If stack is empty
if (st.empty())
{
// There is no 1 that can
// change this 0 to 1
if (S.charAt(i) == '0')
{
flag = 0;
break;
}
// Push 1 into the stack
count = 0;
st.push(S.charAt(i));
}
else
{
if (S.charAt(i) == '0')
{
count++;
// The last 1 has reached
// its limit
if (count == K)
{
st.pop();
count = 0;
}
}
// New 1 has been found which
// can now change at most K 0s
else
{
count = 0;
}
}
}
// If flag is 1, print "YES"
// else print "NO"
if (flag == 1)
System.out.print("YES");
else
System.out.print("NO");
}
// Driver code
public static void main(String args[])
{
// Given Input
String S = "100100";
int N = S.length();
int K = 2;
// Function call
changeCharacters(S, N, K);
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program for the above approach
# Function to check whether all 0s
# in the string can be changed into 1s
def changeCharacters(S, N, K):
flag = 1
# Store the count of 0s converted
# for the last occurrence of 1
count = 0
# Declere a stack
st = []
# Traverse the string, S
for i in range(N):
# If stack is empty
if len(st) == 0:
# There is no 1 that can
# change this 0 to 1
if (S[i] == '0'):
flag = 0
break
# Push 1 into the stack
count = 0
st.append(S[i])
else:
if (S[i] == '0'):
count+=1
# The last 1 has reached
# its limit
if (count == K):
del st[-1]
count = 0
# New 1 has been found which
# can now change at most K 0s
else:
count = 0
# If flag is 1, pr"YES"
# else pr"NO"
if (flag):
print("YES")
else:
print("NO")
# Driver code
if __name__ == '__main__':
# Given Input
S = "100100"
N = len(S)
K = 2
# Function call
changeCharacters(S, N, K)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check whether all 0s
// in the string can be changed into 1s
static void changeCharacters(string S, int N, int K)
{
int flag = 1;
// Store the count of 0s converted
// for the last occurrence of 1
int count = 0;
// Declere a stack
Stack st = new Stack();
// Traverse the string, S
for(int i = 0; i < N; i++) {
// If stack is empty
if (st.Count==0) {
// There is no 1 that can
// change this 0 to 1
if (S[i] == '0') {
flag = 0;
break;
}
// Push 1 into the stack
count = 0;
st.Push(S[i]);
}
else {
if (S[i] == '0') {
count++;
// The last 1 has reached
// its limit
if (count == K) {
st.Pop();
count = 0;
}
}
// New 1 has been found which
// can now change at most K 0s
else {
count = 0;
}
}
}
// If flag is 1, print "YES"
// else print "NO"
if (flag == 1)
Console.Write("YES");
else
Console.Write("NO");
}
// Driver code
public static void Main()
{
// Given Input
string S = "100100";
int N = S.Length;
int K = 2;
// Function call
changeCharacters(S, N, K);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
YES
时间复杂度: O(N)
辅助空格: O(1),因为在任何时候堆栈中最多存在一个字符