Help me in solving SMILEY problem

My issue

can anyone help me to find why my code is showing wrong answer after submitting it.

My code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    string s;
	    cin>>s;
	    int i=0;
	    while(s[i]!=':'){
	        i++;
	    }
	    
	    long long left=0,right=0,ans=0;
	    for(int k=i;k<n;k++){
	        if(s[k]==')'){
	            right++;
	        }
	        else if(s[k]=='('){
	            left++;
	        }
	        else if(right>0 && left==0 && s[k]==':'){
	            ans++;
	            left=0;
	            right=0;
	            
	        }
	    }
	    cout<<ans<<endl;
	}
	return 0;
}

Problem Link: SMILEY Problem - CodeChef

@mayank_raj5
I think may be some edge case is missing.
This is my code i have taken care of all the edge cases in much simpler way .
include
using namespace std;

int main() {
// your code goes here

int  t;
cin>>t;
while(t--)
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ans=0;
    for(int i=1;i<n-1;i++)
    {
        if(s[i]==')')
        {
            int prei=i-1;
            while(i<n-1&&s[i]==')')
            {
                i++;
            }
            if(s[prei]==':'&&s[i]==':')
            ans++;
        }
    }
    cout<<ans<<endl;
}

}

1 Like

try test case
n = 6
string = :(:)):
here you solution will give zero but as we can see answer should be one {:)): is a valid answer}
keeping a count of right and left brackets won’t work here.
you should first search β€˜:’ then just after that look for as many occurrences of this β€˜)’ is there and then look for another β€˜:’.
making it more simple you can replace β€˜)…)’ (multiple right brackets) with a single right bracket and do same with β€˜:’ and β€˜(’

ya, i got it. Thanks

Thanks for helping me out.