Given a binary string str, the task is to check whether the given string follows the below condition or not:
- String starts with a ‘1’.
- Each ‘1’ is followed by empty string(“”), ‘1’, or “00”.
- Each “00” is followed by empty string(“”), ‘1’.
If the given string follows the above criteria then print “Valid String” else print “Invalid String”.
Input: str = “1000”
The given string starts with “1” and has “00” followed by the “1” which is not the given criteria.
Hence, the given string is “Invalid String”.
Input: str = “1111”
The given string starts with 1 and has 1 followed by all the 1’s.
Hence, the given string is “Valid String”.
Approach: The idea is to use Recursion. Below are thes steps:
- Check whether 0th character is ‘1’ or not. If it is not ‘1’, return false as the string is not following condition 1.
- To check string satisfying the second condition, recursively call for string starting from 1st index using substr() function in C++.
- To check string satisfying the third condition, first, we need to check if the string length is greater than 2 or not. If yes, then check if ‘0’ is present at the first and second index. If yes, then recursively call for the string starting from 3rd index.
- At any recursive call, If the string is empty, then we have traversed complete string satisfying all the given conditions and print “Valid String”.
- At any recursive call, If the given condition doesn’t satisfy then stop that recursion and print “Invalid String”.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of string.
Auxiliary Space: O(1).
- Check if the string satisfies the given condition
- Check if binary representations of 0 to N are present as substrings in given binary string
- Python | Check if a given string is binary string or not
- Check if a binary string contains consecutive same or not
- Check if all the 1's in a binary string are equidistant or not
- Check divisibility of binary string by 2^k
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Check if a binary string contains all permutations of length k
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check whether the binary equivalent of a number ends with given string or not
- Check if an encoding represents a unique binary string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check whether two strings are equivalent or not according to given condition
- Check if elements of an array can be arranged satisfying the given condition
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Minimum changes required such that the string satisfies the given condition
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.