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 string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if a string follows a^nb^n pattern or not
- Check if the Matrix follows the given constraints or not
- Find N in the given matrix that follows a pattern
- Check whether two strings are equivalent or not according to given condition
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- 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 the string satisfies the given condition
- Python | Check if a given string is binary string or not
- Find the maximum element in an array which is first increasing and then decreasing
- Sum of range in a series of first odd then even natural numbers
- Minimum in an array which is first decreasing then increasing
- Count permutations that are first decreasing then increasing.
- Minimum number of changes such that elements are first Negative and then Positive
- Check if elements of an array can be arranged satisfying the given condition
- Minimum changes required such that the string satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
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.