在给定的二进制字符串中查找所有仅包含 1 的 K 长度子数组
给定一个二进制字符串str[] ,任务是找到所有可能的仅包含1的K长度子数组并打印它们的开始和结束索引。
例子:
Input: str = “0101000”, K=1
Output:
1 1
3 3
Explanation: Substrings at positions 1 and 3 are the substrings with value 1.
Input: str = “11111001”, K=3
Output:
0 2
1 3
2 4
方法:给定的问题可以在滑动窗口技术的帮助下解决。最初创建一个大小为K的窗口,从0到K-1 的计数为 1 。然后从索引1到N-1遍历字符串并减去i-1的值并将i+K的值添加到当前计数。如果当前计数等于K ,则打印i和i+k-1的值作为可能的子数组之一。请按照以下步骤解决问题:
- 将变量cntOf1s初始化为0。
- 使用变量i遍历范围[0, K)并执行以下任务:
- 如果s[i]等于1 ,则将 cntOf1s的值增加1。
- 如果cntOf1s等于K ,则打印当前子字符串作为答案之一。
- 使用变量i遍历范围[1, N)并执行以下任务:
- 如果该位置的字符为 1,则从左侧减少值并从右侧添加变量cntOf1s 。
- 如果cntOf1s的值等于K ,则打印当前子字符串作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all possible
// k length subarrays
void get(string s, int k)
{
int n = s.length();
int cntOf1s = 0;
// Initial window
for (int i = 0; i < k; i++)
if (s[i] == '1')
cntOf1s++;
if (cntOf1s == k)
cout << 0 << " " << k - 1 << endl;
// Traverse the string
for (int i = 1; i < n; i++) {
// Subtract the value from the left and
// add from the right
cntOf1s = cntOf1s - (s[i - 1] - '0')
+ (s[i + k - 1] - '0');
// Check the condition
if (cntOf1s == k)
cout << i << " " << i + k - 1 << endl;
}
}
// Driver Code
int main()
{
string str = "0110101110";
int K = 2;
get(str, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find all possible
// k length subarrays
static void get(char[] s, int k)
{
int n = s.length;
int cntOf1s = 0;
// Initial window
for (int i = 0; i < k; i++)
if (s[i] == '1')
cntOf1s++;
if (cntOf1s == k)
System.out.print(0+ " " + (k - 1 )+"\n");
// Traverse the String
for (int i = 1; i < n && (i + k - 1)
Python3
# python3 program for the above approach
# Function to find all possible
# k length subarrays
def get(s, k):
n = len(s)
cntOf1s = 0
# Initial window
for i in range(0, k):
if (s[i] == '1'):
cntOf1s += 1
if (cntOf1s == k):
print(f"{0} {k - 1}")
# Traverse the string
for i in range(1, n - k + 1):
# Subtract the value from the left and
# add from the right
cntOf1s = cntOf1s - (ord(s[i - 1]) - ord('0')) + \
(ord(s[i + k - 1]) - ord('0'))
# Check the condition
if (cntOf1s == k):
print(f"{i} {i + k - 1}")
# Driver Code
if __name__ == "__main__":
str = "0110101110"
K = 2
get(str, K)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find all possible
// k length subarrays
static void get(char[] s, int k)
{
int n = s.Length;
int cntOf1s = 0;
// Initial window
for (int i = 0; i < k; i++)
if (s[i] == '1')
cntOf1s++;
if (cntOf1s == k)
Console.Write(0 + " " + (k - 1) + "\n");
// Traverse the String
for (int i = 1; i < n && (i + k - 1) < n; i++)
{
// Subtract the value from the left and
// add from the right
cntOf1s = cntOf1s - (s[i - 1] - '0')
+ (s[i + k - 1] - '0');
// Check the condition
if (cntOf1s == k)
Console.Write(i + " " + (i + k - 1) + "\n");
}
}
// Driver Code
public static void Main()
{
String str = "0110101110";
int K = 2;
get(str.ToCharArray(), K);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
1 2
6 7
7 8
时间复杂度: O(N)
辅助空间: O(1)