Maximize non-overlapping Substrings by splitting String into groups with K or (K+1) 1s
Last Updated :
28 Feb, 2023
Given a binary string S of length N and an integer K, the task is to find the maximum number of non-overlapping substrings that can be obtained by splitting it as per the following conditions:
- Each substring of the string contains K or K+1 number of 1.
- At most consecutive K substrings can contain the same number of 1s
Note: If the string does not contain K or K + 1 number of ones, simply neglect that substring.
Examples:
Input: S = “110101000010011011011011000100000”, K = 2
Output: 6
Explanation: The given string can be split into – [110], [10100], [00100110], [110], [110], [11000100000] so the answer is 6.
Input: S = “0000010001001100000110101100110000”, K = 2
Output: 5
Explanation: The given string can be split into – [000001000100], [1100000], [1101], [0110], [0110000] so the answer is 5.
Input: S = “11111111111”, K = 4
Output: 2
Explanation: The string can be split into [1111], [1111] and
The remaining string can be neglected because it does not contain K number of 1s.
Approach: This problem can be solved using the Greedy approach is based on the below idea:
To maximize the count of substrings splitting such that as many substring have count of ‘1’ as K.
As it is not possible to split string having same number of ‘1’ successively more than K times, so after splitting K substrings with count of ‘1’ as K, split a substring with count of ‘1’ as K + 1.
Follow the below steps to implement the approach discussed above:
- Iterate the string from i = 0 to N-1:
- Keep adding the characters into the current substring until the count of 1s is K.
- If consecutive K substrings already had the same number of substrings then move forward and add one more 1 into the current substring and start a new substring.
- Increment the count of the number of substrings.
- If it is not possible to have K number of 1s then ignore that substring.
- Return the final count of substrings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check(string s)
{
int count = 0;
for ( int i = 0; i < s.length(); i++) {
if (s[i] == '1' ) {
count++;
}
}
return count;
}
int MaxSplit(string s, int n, int k)
{
int times = 0;
int k2 = 0;
int ans = 0;
int y;
for ( int i = 0; i < s.length(); i++) {
string x = s.substr(k2, i - k2);
y = check(x);
if (y == k) {
if (times == k) {
continue ;
}
else {
ans += 1;
k2 = i;
times++;
}
}
else if (y == k + 1) {
times = 0;
ans += 1;
k2 = i;
}
}
if (y == k && y == k + 1 && y != 0) {
ans += 1;
}
return ans;
}
int main()
{
string S = "11111111111" ;
int K = 4;
int N = S.length();
cout << MaxSplit(S, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int check(String s)
{
int count = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
count++;
}
}
return count;
}
public static int MaxSplit(String s, int n, int k)
{
int times = 0 ;
int k2 = 0 ;
int ans = 0 ;
int y = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
String x = s.substring(k2, i);
y = check(x);
if (y == k) {
if (times == k) {
continue ;
}
else {
ans += 1 ;
k2 = i;
times++;
}
}
else if (y == k + 1 ) {
times = 0 ;
ans += 1 ;
k2 = i;
}
}
if (y == k && y == k + 1 && y != 0 ) {
ans += 1 ;
}
return ans;
}
public static void main(String[] args)
{
String S = "11111111111" ;
int K = 4 ;
int N = S.length();
System.out.print(MaxSplit(S, N, K));
}
}
|
Python3
def check(s):
count = 0
for i in range ( 0 , len (s)):
if (s[i] = = '1' ):
count + = 1
return count
def MaxSplit(s, n, k):
times = 0
k2 = 0
ans = 0
y = 0
for i in range ( 0 , len (s)):
x = s[k2: k2 + i - k2]
y = check(x)
if (y = = k):
if (times = = k):
continue
else :
ans + = 1
k2 = i
times + = 1
elif (y = = k + 1 ):
times = 0
ans + = 1
k2 = i
if (y = = k and y = = k + 1 and y ! = 0 ):
ans + = 1
return ans
if __name__ = = "__main__" :
S = "11111111111"
K = 4
N = len (S)
print (MaxSplit(S, N, K))
|
C#
using System;
class GFG
{
public static int check(String s)
{
int count = 0;
for ( int i = 0; i < s.Length; i++) {
if (s[i] == '1' ) {
count++;
}
}
return count;
}
public static int MaxSplit(String s, int n, int k)
{
int times = 0;
int k2 = 0;
int ans = 0;
int y = 0;
for ( int i = 0; i < s.Length; i++) {
String x = s.Substring(k2, i-k2);
y = check(x);
if (y == k) {
if (times == k) {
continue ;
}
else {
ans += 1;
k2 = i;
times++;
}
}
else if (y == k + 1) {
times = 0;
ans += 1;
k2 = i;
}
}
if (y == k && y == k + 1 && y != 0) {
ans += 1;
}
return ans;
}
public static void Main () {
String S = "11111111111" ;
int K = 4;
int N = S.Length;
Console.WriteLine(MaxSplit(S, N, K));
}
}
|
Javascript
<script>
function check(s)
{
var count = 0;
for ( var i = 0; i < s.length; i++) {
if (s[i] == '1 ') {
count++;
}
}
return count;
}
// Function to find the minimum operations
// to make the array elements same
function MaxSplit(s, n, k)
{
var times = 0;
var k2 = 0;
var ans = 0;
var y;
// Traversing the string
for (var i = 0; i < s.length; i++) {
// Creating the substring
var x = s.substr(k2, i - k2);
// Checking the count of 1' s in
y = check(x);
if (y == k) {
if (times == k) {
continue ;
}
else {
ans += 1;
k2 = i;
times++;
}
}
else if (y == k + 1) {
times = 0;
ans += 1;
k2 = i;
}
}
if (y == k && y == k + 1 && y != 0) {
ans += 1;
}
return ans;
}
var S = "11111111111" ;
var K = 4;
var N = S.length;
document.write(MaxSplit(S, N, K));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...