From b652f55bad3467ddceb766263e26bb2561703586 Mon Sep 17 00:00:00 2001 From: poojagusain101 <114809750+poojagusain101@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:03:49 +0530 Subject: [PATCH 1/2] Create asteroid_collision.cpp --- asteroid_collision.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 asteroid_collision.cpp diff --git a/asteroid_collision.cpp b/asteroid_collision.cpp new file mode 100644 index 00000000..da751761 --- /dev/null +++ b/asteroid_collision.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + vector asteroidCollision(vector& asteroids) { + vector ans; + int n=asteroids.size(); + for(int i=0;i0 ){ + ans.push_back(curr); + } + else{ + while(!ans.empty() && ans.back()>0 && ans.back() Date: Tue, 31 Oct 2023 23:12:55 +0530 Subject: [PATCH 2/2] Create balanced_parenthesis.cpp --- balanced_parenthesis.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 balanced_parenthesis.cpp diff --git a/balanced_parenthesis.cpp b/balanced_parenthesis.cpp new file mode 100644 index 00000000..118d942e --- /dev/null +++ b/balanced_parenthesis.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +bool isValid(string s) +{ + int n = s.size(); + stack st; + bool ans = true; + for (int i = 0; i < n; i++) + { + if (s[i] == '{' or s[i] == '(' or s[i] == '[') + { + st.push(s[i]); + } + else if (s[i] == ')') + { + if (!st.empty() and st.top() == '(') + { + st.pop(); + } + else + { + ans = false; + break; + } + } + else if (s[i] == ']') + { + if (!st.empty() and st.top() == '[') + { + st.pop(); + } + else + { + ans = false; + break; + } + } + else if (s[i] == '}') + { + if (!st.empty() and st.top() == '{') + { + st.pop(); + } + else + { + ans = false; + break; + } + } + } + if(!st.empty()){ + return false; + } + return ans; +} +int main() +{ + string s = "{([])}"; + + if(isValid(s)){ + cout<<"valid string "; + } + else{ + cout<<"invalid string "; + } + return 0; +}