InterviewSolution
Saved Bookmarks
| 1. |
Check for Balanced Parentheses using Stack |
|
Answer» Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:
Example 1: Below is the source code for C Program to Check for Balanced PARENTHESES using Stack which is successfully compiled and run on Windows System to produce desired output as shown below : int check(char EXP[] ){ int i; char temp; for(i=0;i<strlen(exp);i++) { if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[') PUSH(exp[i]); if(exp[i]==')' || exp[i]=='}' || exp[i]==']') if(top==-1) /*stack empty*/ { printf("Right parentheses are more than left parentheses\n"); return 0; } else { temp=pop(); if(!match(temp, exp[i])) { printf("Mismatched parentheses are : "); printf("%c and %c\n",temp,exp[i]); return 0; } } } if(top==-1) /*stack empty*/ { printf("Balanced Parentheses\n"); return 1; } else { printf("Left parentheses more than right parentheses\n"); return 0; }} |
|