
#define IsLeft(a) (a=='(' || a=='{' || a == '[')
#define IsMatch(a,b) (a=='('&& b==')' \
|| a == '{' && b =='}' \
|| a=='[' && b == ']')

//const char* p: the string that contains Parentheses
//l: the length of the string
bool IsValidParentheses(const char* p, int l)
{
    if (l %2 == 1 || !IsLeft(*p))return false;

    char stack[l];
    int index = 0;
    char c;
    while(*p)
    {
        c = *p++;
        if(IsLeft(c))
            stack[index++] = c;
        else
        {
            //if Parentheses is match, pop stack
            if (IsMatch(stack[index-1],c))
                --index;
            else
                return false;
        }

    }

    //the parentheses is valid if and only if
    //the stack is tempty in finally
    return index <= 0;
}


#include <stdio.h>


int main(int argc, char** argv)
{
    char a[]="[]{}";
    char b[]="{[]()}";
    char c[]="{{{}[]})";
    
    printf("%s is %s Parentheses\n", 
		   a, IsValidParentheses(a,4)?"valid":"invalid");
    printf("%s is %s Parentheses\n",
		   b, IsValidParentheses(b,6)?"valid":"invalid");
    printf("%s is %s Parentheses\n",
		   c, IsValidParentheses(c,8)?"valid":"invalid");

    return 0;
}


