Count of occurrences of a "1(0+)1" pattern in a string
Last Updated :
15 Jul, 2022
Given an alphanumeric string, find the number of times a pattern 1(0+)1 occurs in the given string. Here, (0+) signifies the presence of non empty sequence of consecutive 0's.
Examples:
Input : 1001010001
Output : 3
First sequence is in between 0th and 3rd index.
Second sequence is in between 3rd and 5th index.
Third sequence is in between 5th and 9th index.
So total number of sequences comes out to be 3.
Input : 1001ab010abc01001
Output : 2
First sequence is in between 0th and 3rd index.
Second valid sequence is in between 13th and 16th
index. So total number of sequences comes out to
be 2.
The idea to solve this problem is to first find a '1' and keep moving forward in the string and check as mentioned below:
- If any character other than '0' and '1' is obtained then it means pattern is not valid. So we go on in the search of next '1' from this index and repeat these steps again.
- If a '1' is seen, then check for the presence of '0' at previous position to check the validity of sequence.
Below is the implementation of above idea:
C++
// C++ program to calculate number of times
// the pattern occurred in given string
#include<iostream>
using namespace std;
// Returns count of occurrences of "1(0+)1"
// int str.
int countPattern(string str)
{
int len = str.size();
bool oneSeen = 0;
int count = 0; // Initialize result
for (int i = 0; i < len ; i++)
{
// Check if encountered '1' forms a valid
// pattern as specified
if (str[i] == '1' && oneSeen == 1)
if (str[i - 1] == '0')
count++;
// if 1 encountered for first time
// set oneSeen to 1
if (str[i] == '1' && oneSeen == 0)
{
oneSeen = 1;
continue;
}
// Check if there is any other character
// other than '0' or '1'. If so then set
// oneSeen to 0 to search again for new
// pattern
if (str[i] != '0' && str[i] != '1')
oneSeen = 0;
}
return count;
}
// Driver program to test above function
int main()
{
string str = "100001abc101";
cout << countPattern(str);
return 0;
}
Java
//Java program to calculate number of times
//the pattern occurred in given string
public class GFG
{
// Returns count of occurrences of "1(0+)1"
// int str.
static int countPattern(String str)
{
int len = str.length();
boolean oneSeen = false;
int count = 0; // Initialize result
for(int i = 0; i < len ; i++)
{
char getChar = str.charAt(i);
// Check if encountered '1' forms a valid
// pattern as specified
if (getChar == '1' && oneSeen == true){
if (str.charAt(i - 1) == '0')
count++;
}
// if 1 encountered for first time
// set oneSeen to 1
if(getChar == '1' && oneSeen == false)
oneSeen = true;
// Check if there is any other character
// other than '0' or '1'. If so then set
// oneSeen to 0 to search again for new
// pattern
if(getChar != '0' && str.charAt(i) != '1')
oneSeen = false;
}
return count;
}
// Driver program to test above function
public static void main(String[] args)
{
String str = "100001abc101";
System.out.println(countPattern(str));
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python program to calculate number of times
# the pattern occurred in given string
# Returns count of occurrences of "1(0+)1"
def countPattern(s):
length = len(s)
oneSeen = False
count = 0 # Initialize result
for i in range(length):
# Check if encountered '1' forms a valid
# pattern as specified
if (s[i] == '1' and oneSeen):
if (s[i - 1] == '0'):
count += 1
# if 1 encountered for first time
# set oneSeen to 1
if (s[i] == '1' and oneSeen == 0):
oneSeen = True
# Check if there is any other character
# other than '0' or '1'. If so then set
# oneSeen to 0 to search again for new
# pattern
if (s[i] != '0' and s[i] != '1'):
oneSeen = False
return count
# Driver code
s = "100001abc101"
print(countPattern(s))
# This code is contributed by Sachin Bisht
C#
// C# program to calculate number
// of times the pattern occurred
// in given string
using System;
class GFG
{
// Returns count of occurrences
// of "1(0+)1" int str.
public static int countPattern(string str)
{
int len = str.Length;
bool oneSeen = false;
int count = 0; // Initialize result
for (int i = 0; i < len ; i++)
{
char getChar = str[i];
// Check if encountered '1' forms
// a valid pattern as specified
if (getChar == '1' &&
oneSeen == true)
{
if (str[i - 1] == '0')
{
count++;
}
}
// if 1 encountered for first
// time set oneSeen to 1
if (getChar == '1' &&
oneSeen == false)
{
oneSeen = true;
}
// Check if there is any other character
// other than '0' or '1'. If so then set
// oneSeen to 0 to search again for new
// pattern
if (getChar != '0' &&
str[i] != '1')
{
oneSeen = false;
}
}
return count;
}
// Driver Code
public static void Main(string[] args)
{
string str = "100001abc101";
Console.WriteLine(countPattern(str));
}
}
// This code is contributed
// by Shrikant13
PHP
<?php
// PHP program to calculate number of times
// the pattern occurred in given string
// Returns count of occurrences
// of "1(0+)1"
function countPattern($str)
{
$len = strlen($str);
$oneSeen = 0;
$count = 0; // Initialize result
for ($i = 0; $i < $len ; $i++)
{
// Check if encountered '1' forms a
// valid pattern as specified
if ($str[$i] == '1' && $oneSeen == 1)
if ($str[$i - 1] == '0')
$count++;
// if 1 encountered for first
// time set oneSeen to 1
if ($str[$i] == '1' && $oneSeen == 0)
$oneSeen = 1;
// Check if there is any other character
// other than '0' or '1'. If so then set
// oneSeen to 0 to search again for new
// pattern
if ($str[$i] != '0' && $str[$i] != '1')
$oneSeen = 0;
}
return $count;
}
// Driver Code
$str = "100001abc101";
echo countPattern($str);
// This code is contributed
// by ChitraNayal
?>
JavaScript
<script>
//Javascript program to calculate number of times
//the pattern occurred in given string
// Returns count of occurrences of "1(0+)1"
// int str.
function countPattern(str)
{
let len = str.length;
let oneSeen = false;
let count = 0; // Initialize result
for(let i = 0; i < len ; i++)
{
let getChar = str[i];
// Check if encountered '1' forms a valid
// pattern as specified
if (getChar == '1' && oneSeen == true){
if (str[i-1] == '0')
count++;
}
// if 1 encountered for first time
// set oneSeen to 1
if(getChar == '1' && oneSeen == false)
oneSeen = true;
// Check if there is any other character
// other than '0' or '1'. If so then set
// oneSeen to 0 to search again for new
// pattern
if(getChar != '0' && str[i] != '1')
oneSeen = false;
}
return count;
}
// Driver program to test above function
let str = "100001abc101";
document.write(countPattern(str));
//This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O( N ), where N is the length of input string.
Auxiliary Space: O(1), as extra space is not required
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].
Similar Reads
Find all the patterns of "1(0+)1" in a given string (General Approach) A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern.
5 min read
Count of substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro
6 min read
Count Substrings with number of 0s and 1s in ratio of X : Y Given a binary string S, the task is to count the substrings having the number of 0s and 1s in the ratio of X : Y Examples: Input: S = "010011010100", X = 3, Y = 2Output: 5Explanation: The index range for the 5 substrings are: (0, 4), (2, 6), (6, 10), (7, 11), (2, 11) Input: S = "10010101", X = 1, Y
7 min read
Find all the patterns of "1(0+)1" in a given string using Regular Expression In Set 1, we have discussed general approach for counting the patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0âs.In this post, we will discuss regular expression approach to count the same. Examples: Input : 1101001 Output : 2 Input : 100001abc101 Output : 2
3 min read
Count number of binary strings of length N having only 0's and 1's Given an integer N, the task is to count the number of binary strings of length N having only 0's and 1's. Note: Since the count can be very large, return the answer modulo 10^9+7. Examples: Input: 2 Output: 4 Explanation: The numbers are 00, 01, 11, 10. Hence the count is 4. Input: 3 Output: 8 Expl
6 min read