1[0]1 Pattern Count | Practice | GeeksforGeeks

Given a string S, 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.

Example 1:
Input:
S = "100001abc101"
Output: 2
Explanation:
The two patterns are "100001" and "101".

Example 2:
Input:
S = "1001ab010abc01001"
Output: 2
Explanation:
The two patterns are "1001"(at start)
and "1001"(at end).

User Task:
Your task is to complete the function patternCount() which takes a single argument(string S) and returns the count of patterns. You need not take any input or print anything.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).

Constraints:
1<=Length of String<=10^5
0
surjit200120012 months ago
C++
int patternCount(string S) 
        { 
            int n=S.length();
            int prev=-1;
            int res=0;
            for(int i=0;i<n;i++){
                if(S[i]=='1'){
                    if(prev!=-1){
                        if(i-prev>1)res++;
                    }
                    prev=i;
                }else if(S[i]!='0'){
                    prev=-1;
                }
            }
            return res;
        } 
0
mayank20213 months ago
C++int patternCount(string S)        {        int count=0,i;       for(i=0; i<S.size();i++)       {           if(S[i]=='1')           {               if(S[i+1]!='0')               continue;               else               {               while(S[i+1]=='0')                   i=i+1;               i=i+1;               }                              if(S[i]=='1')               {                   i=i-1;                 count++;               }                          }       }              return count;
       }  
0
kushbooagarwal35844 months ago
class Solution
{
    public:
        int patternCount(string s) 
        { 
            //code here.
            int zeroes = 0,count = 0;
            int i = 0;
            while(i<s.size()){
                if(s[i] == '1'){
                    while(s[i+1] == '0'){
                        zeroes++;
                        i++;
                    }
                    if(s[i+1] == '1' && zeroes >= 1){
                        count++;
                    }
                   zeroes = 0;
                }
               i++;
            }
            return count;
        } 

};
0
tanujyoti9997 months ago
class Solution
{
    public:
        int patternCount(string s) 
        { 
            int i=0, cnt=0;
            int n=s.length();
            while(i<n)
            {
                while(i<n&& s[i]!='1') i++;
                while(i<n && s[i]=='1') i++;
                while(i<n && s[i]=='0') i++;
                if(i<n && s[i]=='1') cnt++;
            }
            return cnt;
            //code here.
        } 

};
0
ANKIT SINHA8 months ago
ANKIT SINHA
 int patternCount(string s)         {             int i,n=s.size(),t=0;            for(i=0;i<n;) {="" if(s[i]="='1')" {="" int="" c="0;" i++;="" while(s[i]="='0')" {="" i++;="" c++;="" }="" if(c="">0 && s[i]=='1')                    t++;                }                else                i++;            }            return t;        }https://uploads.disquscdn.c...
0
ANKIT SINHA
This comment was deleted.
0
pk verma10 months ago
pk verma
class Solution{    public:        int patternCount(string s)         {            int ans=0;            for(int i=0;i<s.size();i++) {="" if(s[i]="='1'" &&="" s[i+1]="='0'" &&="" i+1<s.size())="" {="" int="" j="i+1;" while(s[j]="='0'" &&="" j<s.size())="" j++;="" if(s[j]="='1')" ans++;="" i="j-1;" }="" }="" return="" ans;="" }="" };<="" spoiler="">
0
Jyoti11 months ago
Jyoti
0
Ravi Kumar1 year ago
Ravi Kumar
Correct AnswerC++
int patternCount(string s)     {         int cnt = 0;        for(int i = 0; i < s.length()-1;)        {            bool flag1 = false;          if(s[i] == '1')          {              flag1 = true;              i++;              bool flag = false;              while(s[i] != '1')              {                  if(s[i] == '0')                    flag = true;                  else if(s[i] != '0' && s[i] != '1')                      {flag = false;break;}                  i++;              }              if(flag == true)                cnt++;          }          if(flag1 == false)            i++;        }        return cnt;    }
0
Himanshu Aswal1 year ago
Himanshu Aswal
C++ O(N) SOLUTION
class Solution{public:  int patternCount(string s)  {    int n = s.size();    int sum = 0;    bool o1 = 0;    int zero = 0;    for (int i = 0; i < n; i++) {      if (s[i] == '1') {        if (zero == 0) {          o1 = 1;        }        else if (zero != 0 and o1) {          sum++;          zero = 0;        }      }      else if (s[i] == '0' and o1) {        zero++;      }      else {        o1 = 0;        zero = 0;      }    }    return sum;  }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.