1[0]1 Pattern Count

PROBLEM :
Given a string, your task is to find the number of patterns of form 1[0]1 where [0] represents any number of zeroes (minimum requirement is one 0) there should not be any other character except 0 in the [0] sequence.

Input:
The first line contains T denoting the number of testcases. Then follows description of testcases. Each case contains a string.

Output:
For each test case, output the number of patterns.

Constraints:
1<=T<=20
1<=Length of String<=2000

Example:
Input:
2
100001abc101
1001ab010abc01001

Output:
2
2

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
int PatternCount(string ) ;
int main()
{
int t ;
string str ;
cin>>t ;
while(t--){
   cin>>str ;
   cout<<PatternCount(str)<<endl ;
}
return 0;
}

int PatternCount(string str){
    bool flag=false ;
    int len,count ;
    len=str.length() ;
    count=0 ;
   
    for(int i=0;i<len;i++){
        if(str[i]=='1'&&flag==false)
            flag=true ;
        else if(str[i]!='0'&&str[i]!='1')
            flag=false ;
        else if(str[i]=='1'&&flag==true){
            if(str[i-1]=='0')
                count++ ;
        }
    }
    return count ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )