Created
January 3, 2024 13:35
-
-
Save apurvanandan1997/16fa36539ca14cbd0406e520e53209bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isValid(char* s) { | |
int stack[10000]; | |
int top=-1; | |
for(int i = 0; i < strlen(s); i++) | |
{ | |
switch(s[i]) | |
{ | |
case '(' : stack[++top] = 1; | |
continue; | |
case '{' : stack[++top] = 2; | |
continue; | |
case '[' : stack[++top] = 3; | |
continue; | |
case ')' : if (top <0 || stack[top] != 1) | |
return false; | |
else | |
top--; | |
continue; | |
case '}' : if (top < 0 || stack[top] != 2) | |
return false; | |
else | |
top--; | |
continue; | |
case ']' : if (top < 0 || stack[top] != 3) | |
return false; | |
else | |
top--; | |
continue; | |
} | |
} | |
return (top==-1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment