Easy approach by using a stack to find longest valid parentheses using C
Approach\n Describe your approach to solving the problem. \n\nThe approach relies on a stack to track the indices of \'(\' characters. While traversing the stri
# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe approach relies on a stack to track the indices of \'(\' characters. While traversing the string, \'(\' indices are pushed onto the stack. When encountering \')\', the top index is popped. The maximum valid substring length is updated accordingly by subtracting indices.\n# Code\n```\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define MAX(a, b) ((a) > (b) ? (a) : (b))\n\nint longestValidParentheses(char* s) {\n    int max_length = 0;\n    int* stack = (int*)malloc(sizeof(int) * (strlen(s) + 1));\n    int top = -1;\n\n    stack[++top] = -1;\n\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (s[i] == \'(\') {\n            stack[++top] = i;\n        } else {\n            top--;\n            if (top == -1) {\n                stack[++top] = i; \n            } else {\n                max_length = MAX(max_length, i - stack[top]);\n            }\n        }\n    }\n\n    free(stack);\n    return max_length;\n}\n```
['C']