TechoSagar: Check Binary String

Check Binary String

Given a binary string of 0 and 1, write a program to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1’s. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not.

Input:
First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of each test case contains a binary string.

Output:
For each test case print "VALID" without quotes if the string is valid otherwise print "INVALID" without quotes if the string is invalid.

Constraints:
1<=T<=100
2<=length(Str)<=105

Example:
Input:
3
100
1110001
00011
Output:
VALID
INVALID
VALID

Your Code

#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.length();
int i;
int temp=1;
int j;
for(i=0; i<l-2; i++)
{
if(s[i]=='1')
{
if(s[i+1]=='0')
{
  for(j=i+2; j<l; j++)
  {
  if(s[j]=='1')
  {
  temp=0;
     break;
  }
  }
}
}
}
if(temp==1)
{
cout<<"VALID"<<endl;

}
else
cout<<"INVALID"<<endl;
}

}

Follow us

Follow Bijendra Kumar on Facebook