Valid String | Practice | GeeksforGeeks

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 <= 104
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.
0
dipawalimandaokar3212 months ago
Why this solution is wrong?
#include <iostream>using namespace std;
void is_valid(string s,int n){   int c1=0,c0=0;for(int i=0;i<n;i++){  if( s[i]=='0')  c0++;  else  c1++;}if(c1==c0)cout<<"yes"<<endl;elsecout<<"no"<<endl;}
int main() {int t,n;string s;cin>>t;while(t--){   cin>>n;   cin>>s;      if(n%2!=0)   cout<<"no";   else   is_valid(s,n);   }    return 0;}
0
dipawalimandaokar3212 months ago
What's wrong with this code?please tell me!
#include <iostream>using namespace std;bool is_valid_string(string str,int N){int count0,count1; for(int i=0;i<N;i++){ if(str[i]=='0') count0++;else count1++;}
if(count0!=count1)return false;
else return true;
 
}int main() {//codeint T,N ;string str ;
cin>>T;while(T--){cin>>N;cin>>str;
if(is_valid_string(str,N))cout<<"yes"<<endl;elsecout<<"no"<<endl;
 
}
return 0;}
0
snipperwolf3 months ago
for _ in range(int(input())):   n=int(input())   s=input()   flag=1   count,count1=0,0   if s.count('0')==s.count('1'):       for i in s:           if i=='0':               count+=1           if i=='1':               count1+=1           if count>=count1:               continue           else:               flag=-1               break       if flag== -1:           print("no")       else:           print("yes")   else:       print("no")
0
chessnoobdj3 months ago
C++ without extra space
#include<iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n, cnt_zero = 0, cnt_one = 0;
        cin >> n;
        bool flg = true;
        for(int i=0; i<n; i++){
            char x;
            cin >> x;
            cnt_zero += (x == '0') ? 1 : 0;
            cnt_one  += (x == '0') ? 0 : 1;
            flg = (cnt_zero < cnt_one) ? false : flg;
        }
        cout << ((flg && cnt_zero==cnt_one) ? "yes" : "no") << "\n";
    }
    return 0;
}
0
avinav26113 months ago
Easiest C++ Solution
 
0
raunakmishra12435 months ago
#include<stack>
using namespace std;
int valid(string s)
{
    
    stack<char>st;
	for(int i=0;i<s.length();i++)
	{
	    if(s[i]=='0')
	    st.push(s[i]);
	    else 
	    {
	        if(st.empty())
	        return 0;
	        else
	        {
	            st.pop();
	            
	        }
	        
	    }
	   
	}
    if(st.empty())
    return 1;
    return 0;
    
}
int main() {
	stack<int>s;
	int t;
	cin>>t;
	while(t--)
	{ 
	    int n;
	    cin>>n;
	    string s;
	    cin>>s;
	    if(valid(s))
	    cout<<"yes"<<endl;
	    else
	    cout<<"no"<<endl;
   }
	return 0;
}
0
imranwahid6 months ago
Easy C++ solution
0
Mayank Manik
This comment was deleted.
0
Mayank Manik
This comment was deleted.
0
Om Bohare10 months ago
Om Bohare
// FOR JAVA TRY THIS // JUST 0.3 SEC //******************************************************//
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
  int n = sc.nextInt();  sc.nextLine();
  String s = sc.nextLine();
  Stack<character> st = new Stack<>();  boolean q = true;  for(int i = 0;i < n; i++){      char ch = s.charAt(i);      if(ch == '0'){          st.push(ch);      }else{
          if(st.size() > 0){              st.pop();
          }else{              q = false;              break;          }      }  }
  if(st.size() == 0 && q == true){      System.out.println("yes");  }else{      System.out.println("no");  }
    t--;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.