Find all the patterns of “1(0+)1” in a given string (General Approach)
Last Updated :
28 Jan, 2023
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.
One approach to solve the problem is discussed here, other using Regular expressions is given in Set 2
Examples:
Input : 1101001
Output : 2
Input : 100001abc101
Output : 2
Let size of input string be n.
- Iterate through index ‘0’ to ‘n-1’.
- If we encounter a ‘1’, we iterate till the elements are ‘0’.
- After the stream of zeros ends, we check whether we encounter a ‘1’ or not.
- Keep on doing this till we reach the end of string.
Below is the implementation of the above method.
Java
import java.io.*;
class GFG
{
static int patternCount(String str)
{
char last = str.charAt( 0 );
int i = 1 , counter = 0 ;
while (i < str.length())
{
if (str.charAt(i) == '0' && last == '1' )
{
while (i < str.length() && str.charAt(i) == '0' )
i++;
if (i == str.length()) {
break ;
}
if (str.charAt(i) == '1' )
counter++;
}
if (i == str.length()) {
break ;
}
last = str.charAt(i);
i++;
}
return counter;
}
public static void main (String[] args)
{
String str = "1001ab010abc01001" ;
System.out.println(patternCount(str));
}
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int patternCount(string str)
{
char last = str[0];
int i = 1, counter = 0;
while (i < str.size())
{
if (str[i] == '0' && last == '1' )
{
while (str[i] == '0' )
i++;
if (str[i] == '1' )
counter++;
}
last = str[i];
i++;
}
return counter;
}
int main()
{
string str = "1001ab010abc01001" ;
cout << patternCount(str) << endl;
return 0;
}
|
Python3
def patternCount( str ):
last = str [ 0 ]
i = 1 ; counter = 0
while (i < len ( str )):
if ( str [i] = = '0' and last = = '1' ):
while ( str [i] = = '0' ):
i + = 1
if ( str [i] = = '1' ):
counter + = 1
last = str [i]
i + = 1
return counter
str = "1001ab010abc01001"
ans = patternCount( str )
print (ans)
|
C#
using System;
class GFG
{
static int patternCount(String str)
{
char last = str[0];
int i = 1, counter = 0;
while (i < str.Length)
{
if (str[i] == '0' && last == '1' )
{
while (str[i] == '0' )
i++;
if (str[i] == '1' )
counter++;
}
last = str[i];
i++;
}
return counter;
}
public static void Main ()
{
String str = "1001ab010abc01001" ;
Console.Write(patternCount(str));
}
}
|
PHP
<?php
function patternCount( $str )
{
$last = $str [0];
$i = 1;
$counter = 0;
while ( $i < strlen ( $str ))
{
if ( $str [ $i ] == '0' && $last == '1' )
{
while ( $str [ $i ] == '0' )
$i ++;
if ( $str [ $i ] == '1' )
$counter ++;
}
$last = $str [ $i ];
$i ++;
}
return $counter ;
}
$str = "1001ab010abc01001" ;
echo patternCount( $str ) ;
?>
|
Javascript
<script>
function patternCount(str)
{
var last = str.charAt(0);
var i = 1, counter = 0;
while (i < str.length)
{
if (str.charAt(i) == '0' && last == '1' )
{
while (str.charAt(i) == '0' )
i++;
if (str.charAt(i) == '1')
counter++;
}
last = str.charAt(i);
i++;
}
return counter;
}
var str = "1001ab010abc01001" ;
document.write(patternCount(str));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...