Page 17 of 17

Re: 673 - Parentheses Balance, please see whats the problem here?

Posted: Fri Nov 13, 2015 11:43 am
by Abid_CU

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
    int t,i,j,len,flag;
    char s[5000], tm[5000];
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        gets(s);
        len= strlen(s);
        flag=0;
        j=0;
        for(i=0; i<len; i++)
        {
            if(s[i]=='('||s[i]=='[') tm[j++]= s[i];
            else
            {
                if(s[i]==')')
                {
                    if(tm[j-1]=='(')
                    {
                        j--;
                    }
                    else
                    {
                        printf("No\n");
                        flag=1;
                        break;
                    }
                }
                else if(s[i]==']')
                {
                    if(tm[j-1]=='[')
                    {
                        j--;
                    }
                    else
                    {
                        printf("No\n");
                        flag=1;
                        break;
                    }
                }
            }
        }
        if(j==0 && flag==0) printf("Yes\n");
        else if(j!=0) printf("No\n");
    }
    return 0;
}

Re: 673 - Parentheses Balance

Posted: Thu Jan 07, 2016 1:22 pm
by nasif2587
removed after ac

Re: 673 - Parentheses Balance

Posted: Sun Mar 12, 2017 9:09 pm
by Ishraq_Nibir
What is the problem in my code..getting wrong answer
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,j,k,l,count,mount,len,first,third;
char str[130];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
gets(str);
len=strlen(str);
count=0;mount=0;first=0;third=0;
for(j=0;j<len;j++){
if(str[j]=='('){
first=1;
break;
}
if(str[j]==')'){
first=3;
break;
}
}
for(j=0;j<len;j++){
if(str[j]=='['){
third=1;
break;
}
if(str[j]==']'){
third=3;
break;
}
}
if(first!=3&&third!=3){
for(j=0;j<len;j++){
if(str[j]=='(')
count++;
if(str[j]==')')
count--;
if(str[j]=='[')
mount++;
if(str[j]==']')
mount--;
}
if(count==0&&mount==0)
printf("Yes\n");
else
printf("No\n");
}
else
printf("No\n");
}
return 0;
}

Re: 673 - Parentheses Balance

Posted: Tue Mar 14, 2017 1:38 pm
by lighted
Check your code here. https://www.udebug.com/UVa/673