给定二进制字符串中连续 1 组的计数
给定一个大小为N的二进制字符串S ,任务是仅在字符串S中找到1的组数。
例子:
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
方法:这个问题可以通过遍历字符串的字符来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如count为0 ,它将1的子字符串的数量存储在S中。
- 初始化一个堆栈说st以仅在1 s 的索引之前存储子字符串。
- 使用变量i遍历字符串S的字符并执行以下操作:
- 如果当前字符为1 ,则将1压入堆栈st 。
- 否则,如果st不为空,则将count增加1 。否则清除st 。
- 如果st不为空,则count加1,即如果有后缀1s。
- 最后,打印获得的总计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of the
// groups of 1s only in the binary
// string
int groupsOfOnes(string S, int N)
{
// Stores number of groups of 1s
int count = 0;
// Initialization of the stack
stack st;
// Traverse the string S
for (int i = 0; i < N; i++) {
// If S[i] is '1'
if (S[i] == '1')
st.push(1);
// Otherwise
else {
// If st is empty
if (!st.empty()) {
count++;
while (!st.empty()) {
st.pop();
}
}
}
}
// If st is not empty
if (!st.empty())
count++;
// Return answer
return count;
}
// Driver code
int main()
{
// Input
string S = "100110111";
int N = S.length();
// Function call
cout << groupsOfOnes(S, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.Stack;
class GFG{
// Function to find the number of the
// groups of 1s only in the binary
// string
static int groupsOfOnes(String S, int N)
{
// Stores number of groups of 1s
int count = 0;
// Initialization of the stack
Stack st = new Stack<>();
// Traverse the string S
for(int i = 0; i < N; i++)
{
// If S[i] is '1'
if (S.charAt(i) == '1')
st.push(1);
// Otherwise
else
{
// If st is empty
if (!st.empty())
{
count++;
while (!st.empty())
{
st.pop();
}
}
}
}
// If st is not empty
if (!st.empty())
count++;
// Return answer
return count;
}
// Driver code
public static void main(String[] args)
{
// Input
String S = "100110111";
int N = S.length();
// Function call
System.out.println(groupsOfOnes(S, N));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the number of the
# groups of 1s only in the binary
# string
def groupsOfOnes(S, N):
# Stores number of groups of 1s
count = 0
# Initialization of the stack
st = []
# Traverse the string S
for i in range(N):
# If S[i] is '1'
if (S[i] == '1'):
st.append(1)
# Otherwise
else:
# If st is empty
if (len(st) > 0):
count += 1
while (len(st) > 0):
del st[-1]
# If st is not empty
if (len(st)):
count += 1
# Return answer
return count
# Driver code
if __name__ == '__main__':
# Input
S = "100110111"
N = len(S)
# Function call
print(groupsOfOnes(S, N))
# 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 find the number of the
// groups of 1s only in the binary
// string
static int groupsOfOnes(string S, int N)
{
// Stores number of groups of 1s
int count = 0;
// Initialization of the stack
Stack st = new Stack();
// Traverse the string S
for (int i = 0; i < N; i++) {
// If S[i] is '1'
if (S[i] == '1')
st.Push(1);
// Otherwise
else {
// If st is empty
if (st.Count > 0) {
count++;
while (st.Count > 0) {
st.Pop();
}
}
}
}
// If st is not empty
if (st.Count > 0)
count++;
// Return answer
return count;
}
// Driver code
public static void Main()
{
// Input
string S = "100110111";
int N = S.Length;
// Function call
Console.Write(groupsOfOnes(S, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(N)