📌  相关文章
📜  字符串“ 1(0+)1”模式出现的次数

📅  最后修改于: 2021-04-29 04:31:56             🧑  作者: Mango

给定一个字母数字字符串,找到给定字符串出现模式1(0+)1的次数。在此,(0+)表示存在连续的0的非空序列。

例子:

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.

解决此问题的方法是首先找到一个“ 1”,并在字符串继续前进,并按如下所述进行检查:

  1. 如果获得了除“ 0”和“ 1”以外的任何字符,则表示模式无效。因此,我们继续从该索引中搜索下一个“ 1”,然后再次重复这些步骤。
  2. 如果看到“ 1”,则检查先前位置是否存在“ 0”,以检查序列的有效性。

下面是上述想法的实现:

C++
// C++ program to calculate number of times
// the pattern occurred in given string
#include
using namespace std;
  
// Returns count of occurrences of "1(0+)1"
// int str.
int countPattern(string str)
{
    int len = str.size();
    bool oneSeen = 0;
  
    int count = 0;  // Initialize result
    for (int i = 0; i < len ; i++)
    {
  
        // Check if encountered '1' forms a valid
        // pattern as specified
        if (str[i] == '1' && oneSeen == 1)
            if (str[i - 1] == '0')
                count++;
  
        // if 1 encountered for first time
        // set oneSeen to 1
        if (str[i] == '1' && oneSeen == 0)
           {
            oneSeen = 1;
            continue;
           }
        // Check if there is any other character
        // other than '0' or '1'. If so then set
        // oneSeen to 0 to search again for new
        // pattern
        if (str[i] != '0' && str[i] != '1')
            oneSeen = 0;
  
  
    }
  
    return count;
}
  
// Driver program to test above function
int main()
{
    string str = "100001abc101";
    cout << countPattern(str);
    return 0;
}


Java
//Java program to calculate number of times
//the pattern occurred in given string
public class GFG
{
    // Returns count of occurrences of "1(0+)1"
    // int str.
    static int countPattern(String str)
    {
        int len = str.length();
        boolean oneSeen = false;
          
        int count = 0;  // Initialize result
        for(int i = 0; i < len ; i++)
        {
            char getChar = str.charAt(i);
              
            // Check if encountered '1' forms a valid
            // pattern as specified
            if (getChar == '1' && oneSeen == true){
                if (str.charAt(i - 1) == '0')
                    count++;
            }
  
            // if 1 encountered for first time
            // set oneSeen to 1
            if(getChar == '1' && oneSeen == false)
                oneSeen = true;
              
            // Check if there is any other character
            // other than '0' or '1'. If so then set
            // oneSeen to 0 to search again for new
            // pattern
            if(getChar != '0' && str.charAt(i) != '1')
                oneSeen = false;
              
  
        }
        return count;
    }
  
    // Driver program to test above function
    public static void main(String[] args)
    {
         String str = "100001abc101";
         System.out.println(countPattern(str));
    }
  
}
// This code is contributed by Sumit Ghosh


Python
# Python program to calculate number of times
# the pattern occurred in given string
  
# Returns count of occurrences of "1(0+)1"
def countPattern(s):
    length = len(s)
    oneSeen = False
      
    count = 0   # Initialize result
    for i in range(length):
  
        # Check if encountered '1' forms a valid
        # pattern as specified
        if (s[i] == '1' and oneSeen):
            if (s[i - 1] == '0'):
                count += 1
  
        # if 1 encountered for first time
        # set oneSeen to 1
        if (s[i] == '1' and oneSeen == 0):
            oneSeen = True
   
        # Check if there is any other character
        # other than '0' or '1'. If so then set
        # oneSeen to 0 to search again for new
        # pattern
        if (s[i] != '0' and s[i] != '1'):
            oneSeen = False
   
  
   
    return count
  
# Driver code
s = "100001abc101"
print countPattern(s)
  
# This code is contributed by Sachin Bisht


C#
// C# program to calculate number
// of times the pattern occurred 
// in given string 
using System;
  
class GFG
{
// Returns count of occurrences 
// of "1(0+)1" int str. 
public static int countPattern(string str)
{
    int len = str.Length;
    bool oneSeen = false;
  
    int count = 0; // Initialize result
    for (int i = 0; i < len ; i++)
    {
        char getChar = str[i];
  
        // Check if encountered '1' forms 
        // a valid pattern as specified 
        if (getChar == '1' && 
                 oneSeen == true)
        {
            if (str[i - 1] == '0')
            {
                count++;
            }
        }
  
        // if 1 encountered for first
        // time set oneSeen to 1 
        if (getChar == '1' && 
            oneSeen == false)
        {
            oneSeen = true;
        }
  
        // Check if there is any other character 
        // other than '0' or '1'. If so then set 
        // oneSeen to 0 to search again for new 
        // pattern 
        if (getChar != '0' && 
                 str[i] != '1')
        {
            oneSeen = false;
        }
  
  
    }
    return count;
}
  
// Driver Code
public static void Main(string[] args)
{
    string str = "100001abc101";
    Console.WriteLine(countPattern(str));
}
  
}
  
// This code is contributed 
// by Shrikant13


PHP


输出:

2

时间复杂度:O(N),其中N是输入字符串的长度。