From b5b92ae4c44847e8fb3215f3c4f0dabe919c7cb5 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:02:46 +0530 Subject: [PATCH] Create 1190. Reverse Substrings Between Each Pair of Parentheses --- ...ubstrings Between Each Pair of Parentheses | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1190. Reverse Substrings Between Each Pair of Parentheses diff --git a/1190. Reverse Substrings Between Each Pair of Parentheses b/1190. Reverse Substrings Between Each Pair of Parentheses new file mode 100644 index 0000000..477eb5e --- /dev/null +++ b/1190. Reverse Substrings Between Each Pair of Parentheses @@ -0,0 +1,31 @@ +class Solution { +public: + void reverseString(string &s, int i, int j) + { + while(i < j) + { + if(s[i] == '(') i++; + if(s[j] == ')') j--; + swap(s[i++], s[j--]); + } + } + string reverseParentheses(string s) + { + stackst; + int n = s.size(); + for(int i = 0; i < n; i++) + { + if(s[i] == '(') st.push(i); + if(s[i] == ')') + { + reverseString(s, st.top(), i); + st.pop(); + } + } + + string ans; + for(auto ch:s) + if(isalpha(ch)) ans += ch; + return ans; + } +};