class Solution {
public :
bool isValid ( string s) {
if ( s. empty ( ) )
return true ;
char s1[ 10001 ] ;
int top = - 1 ;
for ( char c : s) {
if ( c == '(' || c == '[' || c == '{' ) {
s1[ ++ top] = c;
}
else {
if ( top == - 1 )
return false ;
if ( c == ')' && s1[ top] != '(' )
return false ;
if ( c == '}' && s1[ top] != '{' )
return false ;
if ( c == ']' && s1[ top] != '[' )
return false ;
top-- ;
}
}
if ( top != - 1 )
return false ;
return true ;
}
} ;
class Solution {
public :
bool isValid ( string s) {
stack< char > st;
if ( s. empty ( ) )
return true ;
for ( char c : s) {
if ( c == '(' || c == '[' || c == '{' )
st. push ( c) ;
else {
if ( st. empty ( ) )
return false ;
if ( c == ')' && st. top ( ) != '(' )
return false ;
if ( c == ']' && st. top ( ) != '[' )
return false ;
if ( c == '}' && st. top ( ) != '{' )
return false ;
st. pop ( ) ;
}
}
if ( ! st. empty ( ) )
return false ;
return true ;
}
} ;