Neat Brackets

/******************************************************************************

                          Online C++ Compiler.
           Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press “Run” button to compile and execute it.

*******************************************************************************/

#include

using namespace std;

int main()
{
string s;
cin>>s;
int l=0,j=0;
if(s.size()<26){
for(int i=0;i<s.size();i++){
if(s[i]==‘(’){
l++;
}
else if(s[i]==‘)’){
j++;
}

}
if(l-j==0){
cout<<“Yes”;
}
else{
cout<<“No”;
}
}

return 0;

}
help

Why Show me Runtime Error??

s = str(input())
result = ‘’
if len(s)/2 - int(len(s)/2) == 0:
mapping = {‘(’: ‘)’}
tem = []
if s[0] == ‘)’:
result = ‘No’
elif s[-1] == ‘(’:
result = ‘No’
else:
for i in s:
if i in ‘(’:
tem.append(mapping[i])
elif i in ‘)’:
if not tem:
result = ‘No’
if tem[-1] == i:
tem.pop()
else:
result = ‘No’
result = ‘Yes’
else:
result = ‘No’
print(result)

#include <stdio.h>
#include <string.h>
int main()
{
    char brac[25];
    int i,len,rem,ocount,ccount;
        len=ocount=ccount=0;
    gets(brac);

        for(i=0;brac[i]!='\0';i++)
        {
            if(brac[i]=='(')
            {
                ocount++;
            }
            else if(brac[i]==')')
            {
                ccount++;
            }

        }

    if(ccount==ocount)
        {
             printf("Yes");
        }
    else
    {
        printf("No");
    }
    return 0;

}

WHAT IS THE PROBLEM HERE?? PLEASE HELP :[

Your code’s only checking if the number of opening and closing brackets are equal.
It won’t give correct output for cases like,

)(

or

)(()

Try to think of a better solution.

1 Like
s=input()

start_p = s.count('(')

end_p = s.count(')')

if (start_p == end_p) and (s[0] != ')' or s[-1] != '('):

    print("Yes")

else:

    print("No")

what is the problem with this condition? getting error at testcase 8

Your code doesn’t give correct output for cases like

())(()

Please try to think of a better solution for the problem.

The uDebug of Neat Brackets isn’t giving correct output for inputs like ())(().
Please fix this issue, @hjr265 bhaiya.

@touhidurrr
@hjr265

#include <iostream>

using namespace std;

int main() {
    string s;
    cin >> s ;
    int n =s.size();
    int a1 = 0 , a2 = 0 ;
    
    for(int i = 0 ; i < n ; i++){
             if(s[i]=='(')
        a1++;
        else if(s[i]==')')
        a2++;
    }
    
    if(a1==a2&&s[0]=='('&&s[n-1]==')')
    cout << "Yes";
    else 
    cout << "No";
    
    
}

whats wrong in this code ;

@tanmoy_03 Your code just seeing the numbers of ‘(’ and ‘)’ are same and the string starts with ‘(’ and ends with ‘)’ or not.
So, your code will print “Yes” for s = "())(()", but the correct answer is “No”.

What is the problem here??Failed at test case 7.

#include<stdio.h>
#include<string.h>

int main(){
    char s[27];
    int i, start = 0, end = 0;

    scanf(" %[^\n]", s);
    int len = strlen(s);
    for(i = 0; i < len; i++){
        if(s[i] == '('){
            start++;
        }
        else if(s[i] == ')'){
            end++;
        }
    }
    if(start == end){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
    return 0;
}

You may have solved this already.

But, what if there are brackets like this: ))((?

1 Like

Yeah, I’d solved this 5 months ago … but I’m astonished that you’ve replied after 7 months…

1 Like
include<stdio.h>
#include<string.h>
int main()
{
    int len, a,b,c;
    char s[25];
    gets(s);
    len = strlen(s);
    b = 0;
    c = 0;
    for(a=0;a<len;a++){
        if(s[a]=='('){
            b++;
        }
        if(s[a]==')'){
            c++;
        }
    }
    if(s[0] == ')' || s[len - 1] == '(' || b != c ){
        printf("No");
    }
    else{
        printf("Yes");
    }

    return 0;
}

Why WA in case 8?

1 Like

Try this input: (()))((())

can you provide me the test input for case 4;
if tried to solve the problem in this way:

#include <bits/stdc++.h>

using namespace std;

int main()
{
string s;
cin>>s;
stackst;
bool flag=true;

for(char val:s)
{

    if(val=='(' || val=='{' || val=='[') st.push(val);
    else if(val==')')
    {
        if(st.top()!='(' || st.empty())
        {

            flag=false;
            break;
        }
        else st.pop();
    }
    else if(val=='}')
    {
        if(st.top()!='{' || st.empty())
        {

            flag=false;
            break;
        }
        else st.pop();

    }
    else if(val==']')
    {
        if(st.top()!=']' || st.empty())
        {

            flag=false;
            break;
        }
        else st.pop();
    }
}



if(st.empty() && flag==true) cout<<"Yes"<<endl;
 else cout<<"No"<<endl;

return 0;

}