Check for 0 Between 1's in Binary String

Last Updated : 25 Jul, 2026

Given a binary string s consisting of '0' and '1', determine whether it is valid such that no '0' appears between two '1's. Return true if valid, otherwise return false.

Examples:Β 

Input: s = "100"
Output: true
Explanation: The string contains only one '1', so no `'0' appears between two '1's', hence it is valid.

Input: s = "1110001"
Output: false
Explanation: The string has '0' occurring between '1's, so it is not valid.

Try It Yourself
redirect icon

[Naive Approach] Check Every 0 - O(n ^ 2) Time and O(1) Space

The idea is to examine every '0' in the string. For each '0', search towards the left to check if there is a '1' before it and search towards the right to check if there is a '1' after it. If both exist, then the '0' lies between two '1's, making the string invalid.

Working of Approach:

  • Traverse the string.
  • For every '0', search left for a '1'.
  • Search right for another '1'.
  • If both are found, return false.
  • Otherwise, continue checking the remaining characters.
C++
#include <bits/stdc++.h>
using namespace std;

bool checkBinary(string &s)
{
    int n = s.size();

    for (int i = 0; i < n; i++)
    {

        if (s[i] == '0')
        {

            bool leftOne = false, rightOne = false;

            // Search on the left
            for (int j = i - 1; j >= 0; j--)
            {
                if (s[j] == '1')
                {
                    leftOne = true;
                    break;
                }
            }

            // Search on the right
            for (int j = i + 1; j < n; j++)
            {
                if (s[j] == '1')
                {
                    rightOne = true;
                    break;
                }
            }

            if (leftOne && rightOne)
                return false;
        }
    }

    return true;
}

int main()
{
    string s = "100";

    cout << (checkBinary(s) ? "true" : "false");

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static boolean checkBinary(String s)
    {
        int n = s.length();

        for (int i = 0; i < n; i++) {

            if (s.charAt(i) == '0') {

                boolean leftOne = false, rightOne = false;

                // Search on the left
                for (int j = i - 1; j >= 0; j--) {
                    if (s.charAt(j) == '1') {
                        leftOne = true;
                        break;
                    }
                }

                // Search on the right
                for (int j = i + 1; j < n; j++) {
                    if (s.charAt(j) == '1') {
                        rightOne = true;
                        break;
                    }
                }

                if (leftOne && rightOne)
                    return false;
            }
        }

        return true;
    }

    public static void main(String[] args)
    {
        String s = "100";

        System.out.println(checkBinary(s) ? "true"
                                          : "false");
    }
}
Python
def checkBinary(s):
    n = len(s)

    for i in range(n):

        if s[i] == '0':

            leftOne = False
            rightOne = False

            # Search on the left
            for j in range(i - 1, -1, -1):
                if s[j] == '1':
                    leftOne = True
                    break

            # Search on the right
            for j in range(i + 1, n):
                if s[j] == '1':
                    rightOne = True
                    break

            if leftOne and rightOne:
                return False

    return True


if __name__ == "__main__":
    s = "100"
    print("true" if checkBinary(s) else "false")
C#
using System;

public class GFG {
    public static bool checkBinary(string s)
    {
        int n = s.Length;

        for (int i = 0; i < n; i++) {
            if (s[i] == '0') {
                bool leftOne = false, rightOne = false;

                // Search on the left
                for (int j = i - 1; j >= 0; j--) {
                    if (s[j] == '1') {
                        leftOne = true;
                        break;
                    }
                }

                // Search on the right
                for (int j = i + 1; j < n; j++) {
                    if (s[j] == '1') {
                        rightOne = true;
                        break;
                    }
                }

                if (leftOne && rightOne)
                    return false;
            }
        }

        return true;
    }

    public static void Main()
    {
        string s = "100";

        Console.WriteLine(checkBinary(s) ? "true"
                                         : "false");
    }
}
JavaScript
function checkBinary(s)
{
    let n = s.length;

    for (let i = 0; i < n; i++) {

        if (s.charAt(i) === "0") {

            let leftOne = false, rightOne = false;

            // Search on the left
            for (let j = i - 1; j >= 0; j--) {
                if (s.charAt(j) === "1") {
                    leftOne = true;
                    break;
                }
            }

            // Search on the right
            for (let j = i + 1; j < n; j++) {
                if (s.charAt(j) === "1") {
                    rightOne = true;
                    break;
                }
            }

            if (leftOne && rightOne)
                return false;
        }
    }

    return true;
}

// Driver Code
let s = "100";
console.log(checkBinary(s) ? "true" : "false");

Output
true

[Expected Approach] Using Two Pointers - O(n) Time and O(1) Space

The idea is to find the first and the last occurrence of '1'. If any '0' exists between these two positions, then that '0' lies between two '1's, so the string is invalid.

Working of Approach:

  • Find the first occurrence of '1'.
  • Find the last occurrence of '1'.
  • Traverse the characters between them.
  • If a '0' is found, return false.
  • Otherwise, return true.
C++
#include <bits/stdc++.h>
using namespace std;

bool checkBinary(string &s)
{
    int left = 0, right = s.size() - 1;

    // Find first '1'
    while (left < s.size() && s[left] == '0')
        left++;

    // Find last '1'
    while (right >= 0 && s[right] == '0')
        right--;

    // Check for '0' between first and last '1'
    for (int i = left; i <= right; i++)
    {
        if (s[i] == '0')
            return false;
    }

    return true;
}

int main()
{
    string s = "100";

    cout << (checkBinary(s) ? "true" : "false");

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static boolean checkBinary(String s)
    {
        int left = 0, right = s.length() - 1;

        // Find first '1'
        while (left < s.length() && s.charAt(left) == '0')
            left++;

        // Find last '1'
        while (right >= 0 && s.charAt(right) == '0')
            right--;

        // Check for '0' between first and last '1'
        for (int i = left; i <= right; i++) {
            if (s.charAt(i) == '0')
                return false;
        }

        return true;
    }

    public static void main(String[] args)
    {
        String s = "100";

        System.out.println(checkBinary(s) ? "true"
                                          : "false");
    }
}
Python
def checkBinary(s):
    left = 0
    right = len(s) - 1

    # Find first '1'
    while left < len(s) and s[left] == '0':
        left += 1

    # Find last '1'
    while right >= 0 and s[right] == '0':
        right -= 1

    # Check for '0' between first and last '1'
    for i in range(left, right + 1):
        if s[i] == '0':
            return False

    return True


if __name__ == "__main__":
    s = "100"
    print("true" if checkBinary(s) else "false")
C#
using System;

public class GFG {
    public static bool checkBinary(string s)
    {
        int left = 0, right = s.Length - 1;

        // Find first '1'
        while (left < s.Length && s[left] == '0')
            left++;

        // Find last '1'
        while (right >= 0 && s[right] == '0')
            right--;

        // Check for '0' between first and last '1'
        for (int i = left; i <= right; i++) {
            if (s[i] == '0')
                return false;
        }

        return true;
    }

    public static void Main()
    {
        string s = "100";

        Console.WriteLine(checkBinary(s) ? "true"
                                         : "false");
    }
}
JavaScript
function checkBinary(s)
{
    let left = 0, right = s.length - 1;

    // Find first '1'
    while (left < s.length && s.charAt(left) === "0")
        left++;

    // Find last '1'
    while (right >= 0 && s.charAt(right) === "0")
        right--;

    // Check for '0' between first and last '1'
    for (let i = left; i <= right; i++) {
        if (s.charAt(i) === "0")
            return false;
    }

    return true;
}

// Driver Code
let s = "100";
console.log(checkBinary(s) ? "true" : "false");

Output
true

[Alternate Approach] Using Regular Expression - O(n) Time and O(1) Space

The idea is to use a regular expression to search for the pattern 1+0+1+, which represents one or more '0's occurring between two groups of '1's. If the pattern is found, the string is invalid.

Working of the Approach

  • Create the regular expression 1+0+1+.
  • Search the string using regex_search().
  • If the pattern is found, return false.
  • Otherwise, return true.
C++
#include <bits/stdc++.h>
using namespace std;

bool checkBinary(string &s)
{

    regex pattern("1+0+1+");

    return !regex_search(s, pattern);
}

int main()
{
    string s = "100";

    cout << (checkBinary(s) ? "true" : "false");

    return 0;
}
Java
import java.util.regex.*;

class GFG {

    static boolean checkBinary(String s)
    {

        Pattern pattern = Pattern.compile("1+0+1+");

        return !pattern.matcher(s).find();
    }

    public static void main(String[] args)
    {

        String s = "100";

        System.out.println(checkBinary(s) ? "true"
                                          : "false");
    }
}
Python
import re


def checkBinary(s):

    pattern = r"1+0+1+"

    return not re.search(pattern, s)


if __name__ == "__main__":

    s = "100"

    print("true" if checkBinary(s) else "false")
C#
using System;
using System.Text.RegularExpressions;

class GFG {
    static bool checkBinary(string s)
    {
        Regex pattern = new Regex("1+0+1+");

        return !pattern.IsMatch(s);
    }

    static void Main()
    {
        string s = "100";

        Console.WriteLine(checkBinary(s) ? "true"
                                         : "false");
    }
}
JavaScript
function checkBinary(s)
{

    let pattern = /1+0+1+/;

    return !pattern.test(s);
}

// Driver code
let s = "100";
console.log(checkBinary(s) ? "true" : "false");

Output
true
Comment