Without Stack brahhhhh


#1

public int solve(String A) {
int n = A.length();
if(n==0)
return 1;
int open=0;
for(int i=0;i<n;i++){
if(A.charAt(i)==’(’)
open++;
else
open–;
// at any given point if we have close brackets greater than open
//then that cannot be balanced further as well.
//so open will be negative in taht case
if(open<0)
return 0;
}
return open==0?1:0;
}


#2

The solution is wrong brahhh…test cases are weak that’s why got AC.
i.e. “()))((()” this is not balanced…but your code is showing it is balanced!


#3

Thanks brahh… just check this out :slight_smile:

public int solve(String A) {
int n = A.length();
if(n==0)
return 1;
// if(A.charAt(0)==’)’ || A.charAt(n-1)==’(’)
// return 0;
int open=0;
for(int i=0;i<n;i++){
if(A.charAt(i)==’(’)
open++;
else
open–;
// at any given point if we have close brackets greater than open
//then that cannot be balanced further as well.
if(open<0)
return 0;
}
return open==0?1:0;
}