Very simple C solution 0ms


  • 0
    V

    bool isValid(char* s)
    {
    char a[10000];
    int i,j,k,ct1=0,ct2=0;
    for(i=0;i<strlen(s);i++)
    {
    switch(s[i])
    {
    case '(': a[ct1]='(';ct1++;break;
    case '{': a[ct1]='{';ct1++;break;
    case '[': a[ct1]='[';ct1++;break;
    case ')': if(a[ct1-1]=='(')ct1--;else ct2++;break;
    case '}': if(a[ct1-1]=='{')ct1--;else ct2++;break;
    case ']': if(a[ct1-1]=='[')ct1--;else ct2++;break;
    }
    }
    if(ct1==0&&ct2==0)
    return true;
    else
    return false;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.