Given a string of 0 and 1, we need 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.
One approach to solve the problem is discussed here, other using Regular expressions is given Set 2
Input : 100 Output : VALID Input : 1110001 Output : NOT VALID There is 0 between 1s Input : 00011 Output : VALID
- Find first occurrence of 1 in string. Let it be first.
- Find last occurrence of 1 in string. Let it be last.
- Run a loop from first to last and return false if there is a 0. If no 0 found, return true.
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Find all the patterns of "1(0+)1" in a given string | SET 1(General Approach)
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Python | Check if a given string is binary string or not
- Check if a string has all characters with same frequency with one variation allowed
- Efficiently check if a string has all unique characters without using any additional data structure
- Check if a string has m consecutive 1's or 0's
- Check if a binary string contains consecutive same or not
- Check if all the 1's in a binary string are equidistant or not
- Check if given Binary string follows then given condition or not
- Check whether the binary equivalent of a number ends with given string or not
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Construct a string that has exactly K subsequences from given string
- Check if a given string is a Reverse Bitonic String or not
- Next word that does not contain a palindrome and has characters from first k
Suppose we have a string: 01111011110000
Now take two variables say A and B. run a loop for 0 to length of the string and point A
to the first occurrence of 1, after that again run a loop from length of the string to 0
and point second variable to the last occurrence of 1.
So A = 1 (Position of first ‘1’ is the string) and B = 9 (last occurrence of ‘1’).
Now run a loop from A to B and check that ‘0’ is present between 1 or not, if “YES” than
set flag to 1 and break the loop, else set flag to 0.
In this case flag will set to 1 as the given string is not valid and print “NOT VALID”.
Time Complexity : O(n)
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Improved By : Sam007