Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
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.
Examples:
Input : 100 Output : VALID Input : 1110001 Output : NOT VALID There is 0 between 1s Input : 00011 Output : VALID
In Set 1, we have discussed general approach for validity of string.In this post, we will discuss regular expression approach for same and it is simple.
As we know that in a string if there is zero between 1’s, than string is not valid.Hence below is one of the regular expression for invalid string pattern.
10+1
So here is the simple regex algorithm.
- Loop over the matcher(string)
- if above regex match is find in the matcher, then string is not valid, otherwise valid.
// Java regex program to check for valid string import java.util.regex.Matcher; import java.util.regex.Pattern; class GFG { // Method to check for valid string static boolean checkString(String str) { // regular expression for invalid string String regex = "10+1"; // compiling regex Pattern p = Pattern.compile(regex); // Matcher object Matcher m = p.matcher(str); // loop over matcher while(m.find()) { // if match found, // then string is invalid return false; } // if match doesn't found // then string is valid return true; } // Driver method public static void main (String[] args) { String str = "00011111111100000"; System.out.println(checkString(str) ? "VALID" : "NOT VALID"); } } |
Output:
VALID
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Recommended Posts:
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a string contains only alphabets in Java using Lambda expression
- std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Python | Check if a given string is binary string or not
- Check if a binary string contains consecutive same or not
- Check divisibility of binary string by 2^k
- Check if all the 1's in a binary string are equidistant or not
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string contains all permutations of length k
- Check if an encoding represents a unique binary string
- Check whether a binary string can be formed by concatenating given N numbers sequentially



