- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of occurrences of a “1(0+)1” pattern in a string in C++
We are given a string str containing 0s,1s and other alphabets . It also contains patterns of the form “1(0+)1” where 0+ means any number (>0) of consecutive 0s. The goal is to find such patterns ( “1(0+)1” ) inside string str.
Let us understand with examples
Input − str = “abb010bb10111011”
Output − Count of occurrences of a “1(0+)1” pattern in a string are − 2
Explanation − The patterns inside str are highlighted: “abb010bb10111011”, “abb010bb10111011”
Input − str = “01001011001001100”
Output − Count of occurrences of a “1(0+)1” pattern in a string are − 4
Explanation − The patterns inside str are highlighted: “01001011001001100”, “01001011001001100”, “01001011001001100”, “01001011001001100”
Approach used in the below program is as follows
It can be seen that all patterns start and end with 1. We will mark the first 1 using a flag variable check=1 and skip all 0s.
For any other character (neither 0 nor 1 ) set check as 0.
If we find another 1 and flag check is 1 then see if the previous value is 0. If yes then increment count as previous 0 is in between two ones. For any non 0 or 1 set check again as 0.
Take input string as str.
Function Pattern_occurrences(string str, int length) takes the string and its length and returns the count of occurrences of a “1(0+)1” pattern in a string
Take the initial count as 0.
Take flag variable check as 0 initially.
Traverse str using for loop from index i=0 to i<length.
If current character str[i] is 1 and check is 0 then set check as 1 and continue.
If current character str[i] is 1 and check is 1. Then it is second 1. Check if previous value str[i-1] is 0. If yes then pattern found. Increment count.
If the current character is neither 0 nor 1 then it will never be part of the pattern. Set check as 0. Now next encountered 1 will be considered as the start of the next pattern (if exists).
At the end count will have a number of such patterns inside str.
Return count as result.
Example
#include<iostream> using namespace std; int Pattern_occurrences(string str, int length){ int count = 0; bool check = 0; for (int i = 0; i < length ; i++){ if (str[i] == '1' && check == 1){ if (str[i - 1] == '0'){ count++; } } if (str[i] == '1' && check == 0){ check = 1; continue; } if (str[i] != '0' && str[i] != '1'){ check = 0; } } return count; } int main(){ string str = "01010111011"; int length = str.length(); cout<<"Count of occurrences of a “1(0+)1” pattern in a string are: "<< Pattern_occurrences(str, length); return 0; }
Output
If we run the above code it will generate the following output −
Count of occurrences of a “1(0+)1” pattern in a string are: 3
- Related Articles
- Count occurrences of a character in a repeated string in C++
- C# program to count occurrences of a word in string
- Find all the patterns of “1(0+)1” in a given string in C++
- Count occurrences of a character in string in Python
- Python program to count occurrences of a word in a string
- Java program to count occurrences of a word in string
- Count occurrences of a string that can be constructed from another given string in C++
- Find the Pattern of 1’s inside 0’s using C++
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- Write a python program to count occurrences of a word in string?
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- How to count the number of occurrences of a character in a string in JavaScript?
- Count Occurrences of Anagrams in C++
- Count number of occurrences for each char in a string with JavaScript?
- Count the number of 1’s and 0’s in a binary array using STL in C++
