Neat Brackets


This is a companion discussion topic for the original entry at https://toph.co/p/neat-brackets

The dataset is poor. No test case with leading ‘)’.

2 Likes

wrong answer in tast case 7.
I think everything is ok.
Whats wrong with me?

#include<stdio.h>
#include<string.h>
int main()
{
    char arr[30];
    gets(arr);
    int i,l,count=0;
    l = strlen(arr);

    for(i=0; i<l; i++)
    {
        if((int) arr[i] == 40)
        {
            count++;
        }
    }
    if(l%2 != 0)
    {
        printf("No\n");
    }
    else if(count == (l/2))
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
    return 0;

}

You are only checking if the number of opening and closing brackets are equal.

Would your code give the correct output for a case like the following?

())(

Even ())( don’t work. all test case fine except 7

#include<stdio.h>
#include<string.h>
char s[26];
int i,a,b,flag=0;

int main()
{
    scanf("%s",&s);
    for(i=0;i<strlen(s);i++) {
        if(s[i]=='(')
           a++;
    if(s[i]==')')
           b++;
        }


    if(s[i]==')())(') {
        printf("No");
    }
    else{
    if(s[i]==')')
        printf("No");
    else if(s[i]==')(')
        printf("No");

    else if(a==b)
        printf("Yes");
    else
        printf("No");

    }
}

You’re code prints Yes for input )()(
Hint: Use stack data structure to solve this problem.

You do not need stack at all to solve the problem.
Try to find out why your code yields the wrong answer for my input at uDebug.

and these lines,

if(s[i]==')')
else if(s[i]==')(')

I don’t know how your code is working or compiling correctly but you cannot compare a character to a string. and a string must be bound with " " this characters not this ' '. I think you should read about strings more.

string is a character array. Its does compile fine, My code finally works with the same processor. tnx

Two additional test cases have been added for this problem to prevent some naive solutions from passing.

1 Like

Try to think of a better/simpler solution.

1 Like

Robbing you the joy of solving a problem wouldn’t be better. :slight_smile:

These are some of the easier problems on Toph. And, they are very straightforward. Try to think about your solutions first and then code. That way you will end up with simpler code most of the time. And, simpler code is easier to debug.

@Quark.1
You can try to run against the test case I had included in uDebug.
The uDebug button can be found at the top of the problem set(s).
You can also click here.

I hope this helps you solve your problem(s).

why my code is getting runtime in tc 8??
here is my code :

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

int main()
{
    stack <char> s;
    string st;
    cin >> st;
    int n=st.size();
    s.push(st[0]);
    for(int i=1;i<n;i++)
    {
        if(s.top()=='(' && st[i]==')')s.pop();
        else s.push(st[i]);
    }
    if(!s.empty())cout << "No\n";
    else cout << "Yes\n";
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
    int i,opb=0,clb=0;
    string s;
    cin>>s;
    if(s[0]!=')' && s[s.length()-1]!='('){
    for(i=0;i<s.length();i++){
        if(s[i]=='('){
           opb++;
        }
        else if(s[i]==')'){
            clb++;
        }
    }
    if(clb==opb){
        cout<<"Yes";
    }
    else{
        cout<<"No";
    }
    }
    else{
        cout<<"No";
    }
    return 0;
}

whats wrong with it?

You are only counting opening and closing brackets. That is not enough. Think deeply and try again.

I think it is not a easy problem anymore. :stuck_out_tongue:
@hjr265 Is there any system to see the testcases? It would be a great help to me if you share the 7th testcases with me. It will help me to find the wrong of my code.
I think if the testcases are more complex, then it should not be in easy catagory anymore.
Some testcases make this problem more harder like:

(()))))((()))((((())
))((((()))))((
)()(
)()()()(
()()()(((())))

I’m stuck with the test case 11, what might that be??

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

int main()
{
  string s;int c=0;
  cin>>s;
  for(int i=0;i<s.size();i++)
  {
    if(s[i]=='(')c++;
    else c--;
  }
  if(c==0)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  c=0;
}

Whats wrong with it?

Your code prints Yes if the input is )( but it should print No.

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

int main()
{
  string s;
  cin>>s;
  int c=0,l=s.size();
  int j=l-1;
  if(l%2!=0||s[0]==')'||s[l-1]=='(')cout<<"No"<<endl;
  else
  {
    for(int i=0;i<l;i++)
    {
        if(s[i]=='(')c++;
        else c--;
    }
    if(c==0)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    c=0;
  }    
}

Test case 8 is wrong answer …