3ms simple C solution


  • 0
    I
    bool isValid(char* s) {
        char *stack = malloc(sizeof(*s) * strlen(s));
        char *start = stack;
        
        while (*s) {
            if (*s == '(' || *s == '{' || *s == '[') {
                *stack = *s;
                stack++;
            } else if ((*s == ')' && *--stack != '(') ||
                       (*s == '}' && *--stack != '{') ||
                       (*s == ']' && *--stack != '[')) {
                return false;
            }
            s++;
        }
        
        return stack == start;
    }
    

Log in to reply
 

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