Wednesday, September 3, 2025

MATCHING PARENTHESIS

 #include <iostream>

#include<stack>
 using namespace std;
 bool isMathcing(char opening,char closing){
    return (opening == '(' && closing == ')') ||
           (opening == '{' && closing == '}') ||
           (opening == '[' && closing == ']');
 }
 bool isBalanced(string str){
    stack<char>st;
    for(char ch:str){
        if(ch=='{'||ch=='('||ch=='['){
            st.push(ch);
        }
        else if (ch == ')' || ch == '}' || ch == ']') {
           
            if (st.empty() || !isMatchingPair(st.top(), ch)) {
                return false;
            }
            st.pop();
    }
 }
  return st.empty();
 }
 int main() {
 string expr;
 cout<<" enter an expression "<<endl;
 cin>>expr;
 if(isBalanced(expr)){
    cout<<" balanced parenthesis "<<endl;

 }else{
    cout<<" unbalanced parenthesis "<<endl;
 }
 return 0;
 }

No comments:

Post a Comment

MergeSort

  #include < iostream >   using namespace std ;   void merge ( vector < int >& n , int low , int mid , int high ){   ...