Check if given Binary string follows then given condition or not

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”.
Examples:

Input: str = “1000”
Output: False
Explanation:
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”
Output: True
Explanation:
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:

1. Check whether 0th character is ‘1’ or not. If it is not ‘1’, return false as the string is not following condition 1.
2. To check string satisfying the second condition, recursively call for string starting from 1st index using substr() function in C++.
3. 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.
4. At any recursive call, If the string is empty, then we have traversed complete string satisfying all the given conditions and print “Valid String”.
5. 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:

C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if the ` `// string follows rules or not ` `bool` `checkrules(string s) ` `{ ` `    ``if` `(s.length() == 0) ` `        ``return` `true``; ` ` `  `    ``// Check for the first condition ` `    ``if` `(s[0] != ``'1'``) ` `        ``return` `false``; ` ` `  `    ``// Check for the third condition ` `    ``if` `(s.length() > 2) { ` `        ``if` `(s[1] == ``'0'` `&& s[2] == ``'0'``) ` `            ``return` `checkrules(s.substr(3)); ` `    ``} ` ` `  `    ``// Check for the second condition ` `    ``return` `checkrules(s.substr(1)); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given String str ` `    ``string str = ``"1111"``; ` ` `  `    ``// Function Call ` `    ``if` `(checkrules(str)) { ` `        ``cout << ``"Valid String"``; ` `    ``} ` `    ``else` `{ ` `        ``cout << ``"Invalid String"``; ` `    ``} ` `    ``return` `0; ` `} `

Java

 `     `  `// Java program for the above approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to check if the ` `// String follows rules or not ` `static` `boolean` `checkrules(String s) ` `{ ` `    ``if` `(s.length() == ``0``) ` `        ``return` `true``; ` ` `  `    ``// Check for the first condition ` `    ``if` `(s.charAt(``0``) != ``'1'``) ` `        ``return` `false``; ` ` `  `    ``// Check for the third condition ` `    ``if` `(s.length() > ``2``) ` `    ``{ ` `        ``if` `(s.charAt(``1``) == ``'0'` `&&  ` `            ``s.charAt(``2``) == ``'0'``) ` `            ``return` `checkrules(s.substring(``3``)); ` `    ``} ` ` `  `    ``// Check for the second condition ` `    ``return` `checkrules(s.substring(``1``)); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Given String str ` `    ``String str = ``"1111"``; ` ` `  `    ``// Function call ` `    ``if` `(checkrules(str))  ` `    ``{ ` `        ``System.out.print(``"Valid String"``); ` `    ``} ` `    ``else` `    ``{ ` `        ``System.out.print(``"Invalid String"``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Python3

 `# Python3 program for the above approach  ` ` `  `# Function to check if the  ` `# string follows rules or not ` `def` `checkrules(s): ` `     `  `    ``if` `len``(s) ``=``=` `0``: ` `        ``return` `True` `         `  `    ``# Check for the first condition ` `    ``if` `s[``0``] !``=` `'1'``: ` `        ``return` `False` `         `  `    ``# Check for the third condition ` `    ``if` `len``(s) > ``2``: ` `        ``if` `s[``1``] ``=``=` `'0'` `and` `s[``2``] ``=``=` `'0'``: ` `            ``return` `checkrules(s[``3``:]) ` `             `  `    ``# Check for the second condition ` `    ``return` `checkrules(s[``1``:]) ` `     `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# Given string ` `    ``s ``=` `'1111'` `     `  `    ``# Function call ` `    ``if` `checkrules(s): ` `        ``print``(``'valid string'``) ` `    ``else``: ` `        ``print``(``'invalid string'``) ` `         `  `# This code is contributed by virusbuddah_ `

Output:

```Valid String
```

Time Complexity: O(N), where N is the length of string.
Auxiliary Space: O(1).

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.