📌  相关文章
📜  在给定的字符串找到“ 1(0+)1”的所有模式| SET 1(一般方法)

📅  最后修改于: 2021-04-28 00:01:45             🧑  作者: Mango

字符串包含形式为1(0+)1的模式,其中(0+)表示任何非空连续的0序列。计算所有这些模式。模式可以重叠。

注意:它仅包含数字和小写字符。该字符串不一定是二进制的。 100201不是有效的模式。
这里讨论一种解决问题的方法,在第2组中给出了使用正则表达式的另一种方法
例子:

Input : 1101001
Output : 2

Input : 100001abc101
Output : 2

令输入字符串的大小为n。
1.遍历索引“ 0”到“ n-1”。
2.如果遇到“ 1”,则迭代直到元素为“ 0”。
3.零流结束后,我们检查是否遇到“ 1”。
4.继续这样做,直到到达字符串的末尾。

下面是上述方法的实现。

C++
/* Code to count 1(0+)1 patterns in a string */
#include 
using namespace std;
  
/* Function to count patterns */
int patternCount(string str)
{
    /* Variable to store the last character*/
    char last = str[0];
  
    int i = 1, counter = 0;
    while (i < str.size())
    {
        /* We found 0 and last character was '1',
          state change*/
        if (str[i] == '0' && last == '1')
        {
            while (str[i] == '0')
                i++;
  
            /* After the stream of 0's, we got a '1',
               counter incremented*/
            if (str[i] == '1')
                counter++;
        }
  
        /* Last character stored */
        last = str[i];
        i++;
    }
  
    return counter;
}
  
/* Driver Code */
int main()
{
    string str = "1001ab010abc01001";
    cout << patternCount(str) << endl;
    return 0;
}


Java
// Java Code to count 1(0+)1 
// patterns in a string 
import java.io.*;
  
class GFG 
{
    // Function to count patterns 
    static int patternCount(String str)
    {
        /* Variable to store the last character*/
        char last = str.charAt(0);
      
        int i = 1, counter = 0;
        while (i < str.length())
        {
            /* We found 0 and last character was '1',
            state change*/
            if (str.charAt(i) == '0' && last == '1')
            {
                while (str.charAt(i) == '0')
                    i++;
      
                // After the stream of 0's, we 
                // got a '1',counter incremented
                if (str.charAt(i) == '1')
                    counter++;
            }
      
            /* Last character stored */
            last = str.charAt(i);
            i++;
        }
      
        return counter;
    }
      
    // Driver Code 
    public static void main (String[] args)
    {
        String str = "1001ab010abc01001";
        System.out.println(patternCount(str));
          
    }
}
  
// This code is contributed by vt_m.


Python3
# Python3 code to count 1(0+)1 patterns in a 
  
# Function to count patterns 
def patternCount(str):
      
    # Variable to store the last character
    last = str[0]
  
    i = 1; counter = 0
    while (i < len(str)):
          
        # We found 0 and last character was '1',
        # state change
        if (str[i] == '0' and last == '1'):
            while (str[i] == '0'):
                i += 1
                  
                # After the stream of 0's, we got a '1',
                # counter incremented
                if (str[i] == '1'): 
                    counter += 1
          
        # Last character stored 
        last = str[i]
        i += 1
      
    return counter
  
  
# Driver Code 
str = "1001ab010abc01001"
ans = patternCount(str)
print (ans)
      
# This code is contributed by saloni1297


C#
// C# Code to count 1(0 + )1 
// patterns in a string 
using System;
  
class GFG 
{
      
    // Function to count patterns 
    static int patternCount(String str)
    {
        // Variable to store the 
        // last character
        char last = str[0];
      
        int i = 1, counter = 0;
        while (i < str.Length)
        {
            // We found 0 and last 
            // character was '1',
            // state change
            if (str[i] == '0' && last == '1')
            {
                while (str[i] == '0')
                    i++;
      
                // After the stream of 0's, we 
                // got a '1',counter incremented
                if (str[i] == '1')
                    counter++;
            }
      
            // Last character stored 
            last = str[i];
            i++;
        }
      
        return counter;
    }
      
    // Driver Code 
    public static void Main ()
    {
        String str = "1001ab010abc01001";
        Console.Write(patternCount(str));
          
    }
}
  
// This code is contributed by nitin mittal


PHP


输出 :

2