Count of groups of consecutive 1s in a given Binary String
Given a binary string S of size N, the task is to find the number of groups of 1s only in the string S.
Examples:
Input: S = “100110111”, N = 9
Output: 3
Explanation:
The following groups are of 1s only:
- Group over the range [0, 0] which is equal to “1”.
- Group over the range [3, 4] which is equal to “11”.
- Group over the range [6, 8] which is equal to “111”.
Therefore, there are a total of 3 groups of 1s only.
Input: S = “0101”
Output: 2
Approach: The problem can be solved by iterating over the characters of the string. Follow the steps below to solve the problem:
- Initialize a variable, say count as 0, which stores the number of substrings of 1s in S.
- Initialize a stack say st to store the substring before an index of 1s only.
- Iterate over the characters of the string S, using the variable i and do the following:
- If the current character is 1, push 1 into stack st.
- Otherwise, If st is not empty, increment count by 1. Else Clear st.
- If st is not empty, increment count by 1, i.e If there is a suffix of 1s.
- Finally, print the total count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int groupsOfOnes(string S, int N)
{
int count = 0;
stack< int > st;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
st.push(1);
else {
if (!st.empty()) {
count++;
while (!st.empty()) {
st.pop();
}
}
}
}
if (!st.empty())
count++;
return count;
}
int main()
{
string S = "100110111" ;
int N = S.length();
cout << groupsOfOnes(S, N) << endl;
return 0;
}
|
Java
import java.util.Stack;
class GFG{
static int groupsOfOnes(String S, int N)
{
int count = 0 ;
Stack<Integer> st = new Stack<>();
for ( int i = 0 ; i < N; i++)
{
if (S.charAt(i) == '1' )
st.push( 1 );
else
{
if (!st.empty())
{
count++;
while (!st.empty())
{
st.pop();
}
}
}
}
if (!st.empty())
count++;
return count;
}
public static void main(String[] args)
{
String S = "100110111" ;
int N = S.length();
System.out.println(groupsOfOnes(S, N));
}
}
|
Python3
def groupsOfOnes(S, N):
count = 0
st = []
for i in range (N):
if (S[i] = = '1' ):
st.append( 1 )
else :
if ( len (st) > 0 ):
count + = 1
while ( len (st) > 0 ):
del st[ - 1 ]
if ( len (st)):
count + = 1
return count
if __name__ = = '__main__' :
S = "100110111"
N = len (S)
print (groupsOfOnes(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int groupsOfOnes( string S, int N)
{
int count = 0;
Stack< int > st = new Stack< int >();
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
st.Push(1);
else {
if (st.Count > 0) {
count++;
while (st.Count > 0) {
st.Pop();
}
}
}
}
if (st.Count > 0)
count++;
return count;
}
public static void Main()
{
string S = "100110111" ;
int N = S.Length;
Console.Write(groupsOfOnes(S, N));
}
}
|
Javascript
<script>
function groupsOfOnes(S, N) {
let count = 0;
var st = [];
for (let i = 0; i < N; i++) {
if (S[i] == '1' )
st.push(1);
else {
if (st.length != 0) {
count++;
while (st.length != 0) {
st.pop();
}
}
}
}
if (st.length != 0)
count++;
return count;
}
var S = "100110111" ;
let N = S.length;
document.write(groupsOfOnes(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
06 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...