Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach) Last Updated : 17 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Implementation: C++ // C++ regex program to check for valid string #include <iostream> #include <regex> using namespace std; // Method to check for valid string bool checkString(string str) { // regular expression for invalid string const regex Regex("10+1"); // if the regex does not match the string // then it is valid return !regex_match(str, Regex); } // Driver method int main() { string str = "00011111111100000"; // Function call cout<<(checkString(str) ? "VALID" : "NOT VALID"); return 0; } // This code is contributed by Aman Kumar Java // 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"); } } Python3 # Python3 regex program to check for valid string import re # Method to check for valid string def checkString(str): # regular expression for invalid string regex = "10+1" x = re.search("10+1", str) return x is None #Driver method str = "00011111111100000" if checkString(str): print("VALID") else: print("NOT VALID") #this code is contributed by phasing17 C# // C# regex program to check for valid string using System; using System.Text.RegularExpressions; class GFG { // Method to check for valid string static bool checkString(string str) { // regular expression for invalid string Regex regex = new Regex(@"10+1"); // if the regex does not match the string // then it is valid return !regex.IsMatch(str); } // Driver method public static void Main(string[] args) { string str = "00011111111100000"; // Function call Console.WriteLine(checkString(str) ? "VALID" : "NOT VALID"); } } // This code is contributed by phasing17 JavaScript // JavaScript regex program to check for valid string // Method to check for valid string function checkString(str) { // returns true if string contains only 0 and 1 // otherwise, returns false return str.match(/^[0-1]+$/) != null; } // Driver method let str = "01"; console.log(checkString(str) ? "VALID" : "NOT VALID"); // This code is contributed by phasing17 OutputVALID Time complexity : O(n) Auxiliary Space : O(1) Comment More infoAdvertise with us Next Article Check if it is possible to rearrange a binary string with alternate 0s and 1s K kartik Improve Article Tags : Strings DSA binary-string java-regular-expression Practice Tags : Strings Similar Reads Check if a binary string has a 0 between 1s or not | Set 1 (General 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 present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. One approach to solving the problem is discussed here, 11 min read Check if it is possible to rearrange a binary string with alternate 0s and 1s Given a binary string of length, at least two. We need to check if it is possible to rearrange a binary string such that there are alternate 0s and 1s. If possible, then the output is YES, otherwise the output is NO. Examples: Input : 1011 Output : NO We can't rearrange the string such that it has a 5 min read Check if all substrings of length K of a Binary String has equal count of 0s and 1s Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print âYesâ. Otherwise, print âNoâ. Examples: Input: S = "101010", K = 2Output: YesExplanation:Since all the substrings of length 6 min read Check if a binary string contains consecutive same or not Given a binary string str consisting of characters '0' and '1'. The task is to find whether the string is valid or not. A string is valid only if the characters are alternating i.e. no two consecutive characters are the same. Examples: Input: str[] = "010101" Output: Valid Input: str[] = "010010" Ou 4 min read Check if a number has bits in alternate pattern | Set-2 O(1) Approach Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate pattern means that the set and unset bits in n occur in alternate order. For example- 5 has an alternate pattern i.e. 101. Print âYesâ if it has an al 6 min read Find all the patterns of "1(0+)1" in a given string (General Approach) A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. 5 min read Like