
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a binary string has two consecutive occurrences of one everywhere in C++
Here we will see another interesting problem. We have to write a code that accepts a string, which has following criteria.
- Every group of consecutive 1s, must be of length 2
- every group of consecutive 1s must appear after 1 or more 0s
Suppose there is a string like 0110, this is valid string, whether 001110, 010 are not valid
Here the approach is simple. we have to find the occurrences of 1, and check whether it is a part of sub-string 011 or not. If condition fails, for any substring then return false, otherwise true.
Example
#include <bits/stdc++.h> using namespace std; bool isValidStr(string str) { int n = str.length(); int index = find(str.begin(), str.end(), '1') - str.begin(); if (index == 0) //when the string starts with 1, then return false return false; while (index <= n - 1) { if (str[index - 1] != '0') // If 1 doesn't appear after an 0 return false; if (index + 1 < n && str[index + 1] != '1') // If '1' is not succeeded by another '1' return false; if (index + 2 < n && str[index + 2] == '1') // If sub-string is of the type "0111" return false; if (index == n - 1) // If str ends with a single 1 return false; index = find(str.begin() + index + 2, str.end(), '1') - str.begin(); } return true; } int main() { string str = "011000110110"; if(isValidStr(str)){ cout << str << " is a valid string"; } else { cout << str << " is NOT a valid string"; } }
Output
011000110110 is a valid string
- Related Articles
- Check if a binary string contains consecutive same or not in C++
- Check if a string has m consecutive 1s or 0s in Python
- Program to check if binary string has at most one segment of ones or not using Python
- Check if a binary string has a 0 between 1s or not in C++
- How to check if a string has at least one letter and one number in Python?
- Check if a string has white space in JavaScript?
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Python - Check if a given string is binary string or not
- Check if a string has all characters with same frequency with one variation allowed in Python
- Check if a Binary Tree (not BST) has duplicate value in C++
- Python program to check if there are K consecutive 1’s in a binary number?
- C# program to check if there are K consecutive 1’s in a binary number
- Check if an encoding represents a unique binary string in Python
- Check if a binary string contains all permutations of length k in C++
- Check If a String Contains All Binary Codes of Size K in C++

Advertisements