Geeksforgeeks

Error

×

Given binary string **str** of size **N** the task is to check if the given string is valid or not. A string is called valid if the number of **0's** equals the number of **1's** and at any moment starting from the left of the string number **0's** must be greater than or equals to the number of **1's**.

**Input: **

1. The first line of the input contains a single integer* * **T** denoting the number of test cases. The description of **T** test cases follows.

2. The first line of each test case contains a single integer** N****.**

3. The next line contains a binary string without spaces.

**Output:** For each test case, print** ****"yes"** if string is valid. Otherwise, print **"no"** (without quotes)

**Constraints:**

1. 1 <= T <= 100

2. 1 <= N <= 10^{4}

3. '0' <= str[i] <= '1'

**Example:
Input:**

2

4

0011

3

001

**Output:**

yes

no

**Explanation:**

**Test Case 1:** String has an equal number of ones and zeros and at each index(starting from left) the number of occurrences of **0's** is greater than or equals to the number of occurrences **1's**.

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Valid String

...