Check if given Binary string follows then given condition or not

• Last Updated : 14 May, 2021

Given 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 the 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 the string satisfying the second condition, recursively call for a string starting from 1st index using substr() function in C++.
3. To check the 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 the 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 notbool checkrules(string s){    if (s.length() == 0)        return true;     // Check for the first condition    if (s != '1')        return false;     // Check for the third condition    if (s.length() > 2) {        if (s == '0' && s == '0')            return checkrules(s.substr(3));    }     // Check for the second condition    return checkrules(s.substr(1));} // Driver Codeint 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 approachimport java.util.*; class GFG{ // Function to check if the// String follows rules or notstatic 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 Codepublic 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 notdef checkrules(s):         if len(s) == 0:        return True             # Check for the first condition    if s != '1':        return False             # Check for the third condition    if len(s) > 2:        if s == '0' and s == '0':            return checkrules(s[3:])                 # Check for the second condition    return checkrules(s[1:])     # Driver codeif __name__ == '__main__':         # Given string    s = '1111'         # Function call    if checkrules(s):        print('valid string')    else:        print('invalid string')         # This code is contributed by virusbuddah_

C#

 // C# program for the above approachusing System; class GFG{ // Function to check if the// String follows rules or notstatic bool checkrules(String s){    if (s.Length == 0)        return true;     // Check for the first condition    if (s != '1')        return false;     // Check for the third condition    if (s.Length > 2)    {        if (s == '0' &&            s == '0')            return checkrules(s.Substring(3));    }     // Check for the second condition    return checkrules(s.Substring(1));} // Driver Codepublic static void Main(String[] args){         // Given String str    String str = "1111";     // Function call    if (checkrules(str))    {        Console.Write("Valid String");    }    else    {        Console.Write("Invalid String");    }}} // This code is contributed by PrinciRaj1992

Javascript


Output:
Valid String

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

My Personal Notes arrow_drop_up