Why this Code gives Wrong Answer?

My issue

My code

#include <bits/stdc++.h>
using namespace std;



int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    string s;
	    cin>>s;
	   
	    int z=0;
	    int o=0;
	    
	    
	    for(int i=0;i<s.length();i++){
	        if(s[i]=='1') o++;
	        else
	        z++;
	    }
	    if(z==1 || o==1 ) cout<<"YES"<<endl;
	    else
	    cout<<"NO"<<endl;
	}
	    

 	return 0;
}

Problem Link: LONGSEQ Problem - CodeChef

@mkaif1617
u have to print yes and no exactly the same as given in the sample test case like “Yes” and “No”.

Ohh,Thanks

The code you provided seems to be solving the problem of determining whether a given binary string has either exactly one ‘0’ or exactly one ‘1’. If that is the problem statement, the code looks correct and should work as expected. It counts the occurrences of ‘0’ and ‘1’ in the input string and then checks if either the count of '0’s or the count of '1’s is equal to 1. If that condition holds, it outputs “YES”, otherwise “NO”.

However, please note that it’s essential to have a clear understanding of the problem statement and the input format to ensure that the code is correct. If you are getting wrong answers, there could be several reasons:

  1. Incorrect Input Format: Ensure that you are providing the input in the correct format as expected by the code.
  2. Loop Iterations or Constraints: Check if the loop iterations are correct and if the code handles all the test cases properly.
  3. Edge Cases: Test the code with corner cases like empty strings, very large strings, or strings with only ‘0’ or ‘1’.
  4. Additional Constraints: Verify if there are any additional constraints in the problem that the code doesn’t account for.

If the input and problem statement are correct, and the code still gives wrong answers, consider providing some example input and expected output for further investigation. Additionally, you may want to cross-verify your code with the problem statement and test it with various test cases to identify any potential issues.

Regards,

Top management consulting firms in delhi