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:
- Check if the first character of the string is ‘1’, if not, return false.
- Traverse the string character by character, starting from the second character.
- If the current character is ‘1’, move to the next character.
- If the current characters are “00”, move two characters ahead and check if the next character is ‘1’, if not, return false.
- If the current character is neither ‘1’ nor “00”, return false.
- If we reach the end of the string without returning false, the string is valid. Return true.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkrules(string s)
{
int n = s.length();
int i = 0;
if (s[i] != '1' ) {
return false ;
}
i++;
while (i < n) {
if (s[i] == '1' ) {
i++;
}
else if (i + 1 < n && s[i] == '0'
&& s[i + 1] == '0' ) {
i += 2;
if (i < n && s[i] != '1' ) {
return false ;
}
}
else {
return false ;
}
}
return true ;
}
int main()
{
string str = "1111" ;
if (checkrules(str)) {
cout << "Valid String" ;
}
else {
cout << "Invalid String" ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean checkrules(String s)
{
int n = s.length();
int i = 0 ;
if (s.charAt(i) != '1' ) {
return false ;
}
i++;
while (i < n) {
if (s.charAt(i) == '1' ) {
i++;
}
else if (i + 1 < n && s.charAt(i) == '0'
&& s.charAt(i + 1 ) == '0' ) {
i += 2 ;
if (i < n && s.charAt(i) != '1' ) {
return false ;
}
}
else {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String str = "1111" ;
if (checkrules(str)) {
System.out.println( "Valid String" );
}
else {
System.out.println( "Invalid String" );
}
}
}
|
Python3
def check_rules(s):
n = len (s)
i = 0
if s[i] ! = '1' :
return False
i + = 1
while i < n:
if s[i] = = '1' :
i + = 1
elif i + 1 < n and s[i] = = '0' and s[i + 1 ] = = '0' :
i + = 2
if i < n and s[i] ! = '1' :
return False
else :
return False
return True
str = "1111"
if check_rules( str ):
print ( "Valid String" )
else :
print ( "Invalid String" )
|
C#
using System;
public class Program
{
public static bool CheckRules( string s)
{
int n = s.Length;
int i = 0;
if (s[i] != '1' )
{
return false ;
}
i++;
while (i < n)
{
if (s[i] == '1' )
{
i++;
}
else if (i + 1 < n && s[i] == '0'
&& s[i + 1] == '0' )
{
i += 2;
if (i < n && s[i] != '1' )
{
return false ;
}
}
else
{
return false ;
}
}
return true ;
}
public static void Main()
{
string str = "1111" ;
if (CheckRules(str))
{
Console.WriteLine( "Valid String" );
}
else
{
Console.WriteLine( "Invalid String" );
}
}
}
|
Javascript
function checkrules(s) {
let n = s.length;
let i = 0;
if (s[i] !== '1' ) {
return false ;
}
i++;
while (i < n) {
if (s[i] === '1' ) {
i++;
}
else if (i + 1 < n && s[i] === '0' && s[i + 1] === '0' ) {
i += 2;
if (i < n && s[i] !== '1' ) {
return false ;
}
}
else {
return false ;
}
}
return true ;
}
let str = "1111" ;
if (checkrules(str)) {
console.log( "Valid String" );
} else {
console.log( "Invalid String" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
bool checkrules(string s)
{
if (s.length() == 0)
return true ;
if (s[0] != '1' )
return false ;
if (s.length() > 2) {
if (s[1] == '0' && s[2] == '0' )
return checkrules(s.substr(3));
}
return checkrules(s.substr(1));
}
int main()
{
string str = "1111" ;
if (checkrules(str)) {
cout << "Valid String" ;
}
else {
cout << "Invalid String" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkrules(String s)
{
if (s.length() == 0 )
return true ;
if (s.charAt( 0 ) != '1' )
return false ;
if (s.length() > 2 )
{
if (s.charAt( 1 ) == '0' &&
s.charAt( 2 ) == '0' )
return checkrules(s.substring( 3 ));
}
return checkrules(s.substring( 1 ));
}
public static void main(String[] args)
{
String str = "1111" ;
if (checkrules(str))
{
System.out.print( "Valid String" );
}
else
{
System.out.print( "Invalid String" );
}
}
}
|
Python3
def checkrules(s):
if len (s) = = 0 :
return True
if s[ 0 ] ! = '1' :
return False
if len (s) > 2 :
if s[ 1 ] = = '0' and s[ 2 ] = = '0' :
return checkrules(s[ 3 :])
return checkrules(s[ 1 :])
if __name__ = = '__main__' :
s = '1111'
if checkrules(s):
print ( 'valid string' )
else :
print ( 'invalid string' )
|
C#
using System;
class GFG{
static bool checkrules(String s)
{
if (s.Length == 0)
return true ;
if (s[0] != '1' )
return false ;
if (s.Length > 2)
{
if (s[1] == '0' &&
s[2] == '0' )
return checkrules(s.Substring(3));
}
return checkrules(s.Substring(1));
}
public static void Main(String[] args)
{
String str = "1111" ;
if (checkrules(str))
{
Console.Write( "Valid String" );
}
else
{
Console.Write( "Invalid String" );
}
}
}
|
Javascript
<script>
function checkrules(s)
{
if (s.length == 0)
return true ;
if (s[0] != '1' )
return false ;
if (s.length > 2) {
if (s[1] == '0' && s[2] == '0' )
return checkrules(s.substring(3));
}
return checkrules(s.substring(1));
}
var str = "1111" ;
if (checkrules(str)) {
document.write( "Valid String" );
}
else {
document.write( "Invalid String" );
}
</script>
|
Time Complexity: O(N), where N is the length of string.
Auxiliary Space: O(1).
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...