

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find all the patterns of “1(0+)1” in a given string in C++
Suppose a string has patterns like 1(0+)1. Where (0+) indicates non-empty consecutive occurrences of 1s. We have to find all of the patterns. The patterns can overlap. The string is not necessarily a binary string. It can hold digits and lowercase characters only. Suppose the string is like 1101001, then there are two such patterns. 101 and 1001.
To solve this problem, we will follow these steps −
Iterate through all character c in the string
When c is 1, then we iterate till the element is 0
When the stream of 0 ends, we will check whether the next character is 1 or not
These steps will be repeated until the end of string is reached.
Example
#include<iostream> using namespace std; int countBinPattern(string main_str) { char last_char = main_str[0]; int i = 1, counter = 0; while (i < main_str.size()) { if (main_str[i] == '0' && last_char == '1') { while (main_str[i] == '0') i++; if (main_str[i] == '1') counter++; } last_char = main_str[i]; i++; } return counter; } int main() { string str = "10010110000101"; cout << "Number of substrings of pattern 1(0+)1 is: " << countBinPattern(str); }
Output
Number of substrings of pattern 1(0+)1 is: 4
- Related Questions & Answers
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- Find all the patterns of "10+1" in a given string using Python Regex
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- Find all divisors of a natural number - Set 1 in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Count of occurrences of a “1(0+)1” pattern in a string in C++
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Program to find number of bit 1 in the given number in Python
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Find the number of divisors of all numbers in the range [1, n] in C++
- Add 1 to a given number?
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
Advertisements