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++
#include<iostream>
using namespace std;
int countPattern(string str)
{
int len = str.size();
bool oneSeen = 0;
int count = 0;
for ( int i = 0; i < len ; i++)
{
if (str[i] == '1' && oneSeen == 1)
if (str[i - 1] == '0' )
count++;
if (str[i] == '1' && oneSeen == 0)
{
oneSeen = 1;
continue ;
}
if (str[i] != '0' && str[i] != '1' )
oneSeen = 0;
}
return count;
}
int main()
{
string str = "100001abc101" ;
cout << countPattern(str);
return 0;
}
|
Java
public class GFG
{
static int countPattern(String str)
{
int len = str.length();
boolean oneSeen = false ;
int count = 0 ;
for ( int i = 0 ; i < len ; i++)
{
char getChar = str.charAt(i);
if (getChar == '1' && oneSeen == true ){
if (str.charAt(i - 1 ) == '0' )
count++;
}
if (getChar == '1' && oneSeen == false )
oneSeen = true ;
if (getChar != '0' && str.charAt(i) != '1' )
oneSeen = false ;
}
return count;
}
public static void main(String[] args)
{
String str = "100001abc101" ;
System.out.println(countPattern(str));
}
}
|
Python3
def countPattern(s):
length = len (s)
oneSeen = False
count = 0
for i in range (length):
if (s[i] = = '1' and oneSeen):
if (s[i - 1 ] = = '0' ):
count + = 1
if (s[i] = = '1' and oneSeen = = 0 ):
oneSeen = True
if (s[i] ! = '0' and s[i] ! = '1' ):
oneSeen = False
return count
s = "100001abc101"
print (countPattern(s))
|
C#
using System;
class GFG
{
public static int countPattern( string str)
{
int len = str.Length;
bool oneSeen = false ;
int count = 0;
for ( int i = 0; i < len ; i++)
{
char getChar = str[i];
if (getChar == '1' &&
oneSeen == true )
{
if (str[i - 1] == '0' )
{
count++;
}
}
if (getChar == '1' &&
oneSeen == false )
{
oneSeen = true ;
}
if (getChar != '0' &&
str[i] != '1' )
{
oneSeen = false ;
}
}
return count;
}
public static void Main( string [] args)
{
string str = "100001abc101" ;
Console.WriteLine(countPattern(str));
}
}
|
PHP
<?php
function countPattern( $str )
{
$len = strlen ( $str );
$oneSeen = 0;
$count = 0;
for ( $i = 0; $i < $len ; $i ++)
{
if ( $str [ $i ] == '1' && $oneSeen == 1)
if ( $str [ $i - 1] == '0' )
$count ++;
if ( $str [ $i ] == '1' && $oneSeen == 0)
$oneSeen = 1;
if ( $str [ $i ] != '0' && $str [ $i ] != '1' )
$oneSeen = 0;
}
return $count ;
}
$str = "100001abc101" ;
echo countPattern( $str );
?>
|
Javascript
<script>
function countPattern(str)
{
let len = str.length;
let oneSeen = false ;
let count = 0;
for (let i = 0; i < len ; i++)
{
let getChar = str[i];
if (getChar == '1' && oneSeen == true ){
if (str[i-1] == '0' )
count++;
}
if (getChar == '1' && oneSeen == false )
oneSeen = true ;
if (getChar != '0' && str[i] != '1' )
oneSeen = false ;
}
return count;
}
let str = "100001abc101" ;
document.write(countPattern(str));
</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 review-team@geeksforgeeks.org.
Share your thoughts in the comments
Please Login to comment...