Check if given Binary string follows then given condition or not
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:
- 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 the string satisfying the second condition, recursively call for a string starting from 1st index using substr() function in C++.
- 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.
- 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”.
- 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 <bits/stdc++.h> 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_ |
C#
// C# program for the above approach using System; class GFG{ // Function to check if the // String follows rules or not static 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.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)) { Console.Write( "Valid String" ); } else { Console.Write( "Invalid String" ); } } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript program for the above approach // Function to check if the // string follows rules or not function checkrules(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.substring(3)); } // Check for the second condition return checkrules(s.substring(1)); } // Driver Code // Given String str var str = "1111" ; // Function Call if (checkrules(str)) { document.write( "Valid String" ); } else { document.write( "Invalid String" ); } </script> |
Valid String
Time Complexity: O(N), where N is the length of string.
Auxiliary Space: O(1).
Please Login to comment...