Monday, January 6, 2025
Google search engine
HomeData Modelling & AIFind all the patterns of “1(0+)1” in a given string using Regular...

Find all the patterns of “1(0+)1” in a given string using Regular Expression

In Set 1, we have discussed general approach for counting the patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s.In this post, we will discuss regular expression approach to count the same. Examples:

Input : 1101001
Output : 2

Input : 100001abc101
Output : 2

Below is one of the regular expression for above pattern

10+1

Hence, whenever we found a match, we increase counter for counting the pattern.As last character of a match will always ‘1’, we have to again start searching from that index. 

Implementation:

C++




#include <iostream>
#include <regex>
 
class GFG
{
public:
    static int patternCount(std::string str)
    {
        // regular expression for the pattern
        std::regex regex("10+1");
 
        // compiling regex
        std::smatch match;
 
        // counter
        int counter = 0;
 
        // whenever match found
        // increment counter
        while (std::regex_search(str, match, regex))
        {
            // As last character of current match
            // is always one, starting match from that index
            str = match.suffix().str();
 
            counter++;
        }
 
        return counter;
    }
};
 
// Driver Method
int main()
{
    std::string str = "1001ab010abc01001";
    std::cout << GFG::patternCount(str) << std::endl;
    return 0;
}


Java




//Java program to count the patterns
// of the form 1(0+)1 using Regex
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG
{
 static int patternCount(String str)
 {
  // regular expression for the pattern
  String regex = "10+1";
   
  // compiling regex
  Pattern p = Pattern.compile(regex);
     
  // Matcher object
  Matcher m = p.matcher(str);
   
  // counter
  int counter = 0;
     
  // whenever match found
  // increment counter
  while(m.find())
  {
   // As last character of current match
   // is always one, starting match from that index
   m.region(m.end()-1, str.length());
    
   counter++;
  }
     
  return counter;
 }
  
 // Driver Method
 public static void main (String[] args)
 {
  String str = "1001ab010abc01001";
  System.out.println(patternCount(str));
 }
}


Python3




# Python program to count the patterns
# of the form 1(0+)1 using Regex
import re
 
def patternCount(str):
    # regular expression for the pattern
    regex = "10+1"
     
    # compiling regex
    p = re.compile(regex)
     
    # counter
    counter=0
     
    # whenever match found
    # increment counter
    for m in re.finditer(p,str):
        counter+=1
     
    return counter
     
# Driver Method
 
str = "1001ab010abc01001"
print(patternCount(str))
 
# This code is contributed by Pushpesh Raj


C#




// C# program to count the patterns
// of the form 1(0+)1 using Regex
using System;
using System.Text.RegularExpressions;
 
class GFG
{
  static int patternCount(String str)
  {
 
    // regular expression for the pattern
    String regex = "10+1";
 
    // compiling regex
    Regex p = new Regex(regex);
 
    // Matcher object
    Match m = p.Match(str);
 
    // counter
    int counter = 0;
 
    // whenever match found
    // increment counter
    while(m.Success)
    {
      // As last character of current match
      // is always one, starting match from that index
      m = m.NextMatch();
      counter++;
    }
 
    return counter;
  }
 
  // Driver Method
  public static void Main (string[] args)
  {
    string str = "1001ab010abc01001";
    Console.WriteLine(patternCount(str));
  }
}
 
// This code is contributed by Aman Kumar


Javascript




// Javascript program for the above approach
 
function patternCount(str) {
  // regular expression for the pattern
  const regex = /10+1/g;
 
  // counter
  let counter = 0;
 
  // whenever match found
  // increment counter
  let match;
  while ((match = regex.exec(str)) !== null) {
    counter++;
  }
 
  return counter;
}
 
// Driver Method
const str = "1001ab010abc01001";
console.log(patternCount(str));
 
// This code is contributed by adityashatmfh


Output

2

Time Complexity: O(n) 
Auxiliary Space: O(1)

Related Articles :

This article is contributed by Gaurav Miglani. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments