Skip to content

Instantly share code, notes, and snippets.

@satyajeetkrjha
Last active July 11, 2022 08:46
Show Gist options
  • Save satyajeetkrjha/771a48b9557bdf9a480e337892d6084b to your computer and use it in GitHub Desktop.
Save satyajeetkrjha/771a48b9557bdf9a480e337892d6084b to your computer and use it in GitHub Desktop.

Revisions

  1. satyajeetkrjha revised this gist Jul 11, 2022. 1 changed file with 49 additions and 15 deletions.
    64 changes: 49 additions & 15 deletions solution.cpp
    Original file line number Diff line number Diff line change
    @@ -2,21 +2,55 @@
    #include <string>
    #include <stack>
    using namespace std;
    int main() {
    string line ;
    string res ;
    while(getline(cin,line)){

    for(int i =0 ; i<line.size();i++){
    char ch = line[i];

    if(ch == '(' || ch == '{' || ch == '[' || ch == ']' || ch == '}' || ch == ')'){
    res+= ch;
    }
    }

    int main() {
    string line;
    string res;
    stack<char> st ;
    while (getline(cin, line)) {
    int sz = line.size();
    for(int i =0 ;i<sz;i++){
    char ch = line[i];
    if(ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == '}' || ch == ']'){
    res+= ch;
    }
    }
    cout << "res is " << res << endl; // moving this at line 17 prints but here it does not print anything



    }
    int ressize = res.size();
    for(int i =0 ;i<ressize;i++){
    char ch = res[i];
    if(ch == '(' || ch == '{' || ch == '['){
    st.push(res[i]);
    }
    else{
    if(st.empty()){
    cout <<"invalid"<< endl;
    return 0;
    }
    char topchar = st.top();
    st.pop();
    if(topchar == '(' && ch !=')'){
    cout <<"invalid"<< endl;
    return 0;
    }
    if(topchar == '{' && ch !='}'){
    cout <<"invalid"<< endl;
    return 0;
    }
    if(topchar == '[' && ch !=']'){
    cout <<"invalid"<<endl;
    return 0;
    }
    }
    }
    if(st.empty()){
    cout <<"valid"<< endl;
    return 0;
    }
    else{
    cout <<"invalid"<< endl;
    return 0;
    }

    }
  2. satyajeetkrjha created this gist Jul 11, 2022.
    22 changes: 22 additions & 0 deletions solution.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #include <iostream>
    #include <string>
    #include <stack>
    using namespace std;
    int main() {
    string line ;
    string res ;
    while(getline(cin,line)){

    for(int i =0 ; i<line.size();i++){
    char ch = line[i];

    if(ch == '(' || ch == '{' || ch == '[' || ch == ']' || ch == '}' || ch == ')'){
    res+= ch;
    }
    }

    }
    cout << "res is " << res << endl; // moving this at line 17 prints but here it does not print anything


    }